Mic*_*ael 5 javascript php wordpress jquery gridster
我有一个问题,我的WordPress插件,我想插入Gridster插件,但它不起作用.
这里我正在加载正确在文件夹中的文件.
function add_my_stylesheet()
{
wp_enqueue_style( 'myCSS', plugins_url( '/css/bootstrap.css', __FILE__ ), false );
wp_enqueue_style("gridster-style", plugins_url( '/css/jquery.gridster.min.css', __FILE__ ), false );
}
add_action('admin_print_styles', 'add_my_stylesheet');
function add_my_scripts()
{
//I tried wp_register_script as well as wp_enqueue_script
wp_register_script( "jquery-gridster", plugins_url( '/js/jquery.min.js', __FILE__ ) );
wp_register_script( "gridster-script", plugins_url( '/js/jquery.gridster.min.js', __FILE__ ) );
wp_register_script( "gridster-script-extra", plugins_url( '/js/jquery. gridster.with-extras.min.js', __FILE__ ) );
}
add_action( 'wp_register_scripts', 'add_my_scripts' );
Run Code Online (Sandbox Code Playgroud)
这是预期输出的代码示例,当然也不起作用.
echo'
<section class="demo">
<div class="gridster">
<ul>
<li class="bg-blue" data-row="1" data-col="1" data-sizex="1" data-sizey="1">Box1</li>
<li class="bg-pink" data-row="1" data-col="2" data-sizex="1" data-sizey="1">Box2</li>
<li class="bg-pink" data-row="1" data-col="3" data-sizex="1" data-sizey="2">Box3</li>
</ul>
</div>
</section>
<script type="text/javascript">
var gridster;
$(function() {
gridtster = $(".gridster > ul").gridster({
widget_margins: [10, 10],
widget_base_dimensions: [140, 140],
min_cols: 6
}).data("gridster");
});
</script>';
Run Code Online (Sandbox Code Playgroud)
我尝试将它包含在模板的头文件和插件中,但它只显示文本,我无法拖放它们.
迈克尔——又是一个好问题!
WordPress 很棒,但它的工作方式有一些技巧/细微差别。
首先,注册脚本很有用(例如,如果您想要本地化它,或者您可能(或可能不)希望在页脚中输出它(使用单独的print_scripts),但是,它不会输出脚本到标记上。
此外,根据您注册它们的位置(在挂钩中wp_register_script
),您可能还想更改它。
如果您只想将脚本输出到页面标记,则按如下方式修改代码:
function add_my_scripts()
{
// proper way to include jQuery that is packaged with WordPress
wp_enqueue_script('jquery');
// Do not EVER include your own jquery. This will cause all sorts of problems for the users of your plugin!
// wp_enqueue_script( "jquery-gridster", plugins_url( '/js/jquery.min.js', __FILE__ ) );
// you need enqueue here. ALSO note the use of the $dependencies parameter - this plugin relies on jquery, so be sure to include that as a dependency!
wp_enqueue_script( "gridster-script", plugins_url( '/js/jquery.gridster.min.js', __FILE__ ), array('jquery') );
wp_enqueue_script( "gridster-script-extra", plugins_url( '/js/jquery. gridster.with-extras.min.js', __FILE__ ), array('gridster-script') );
}
// and you need to hook the enqueue action here...
add_action( 'wp_enqueue_scripts', 'add_my_scripts' );
Run Code Online (Sandbox Code Playgroud)
请注意,如果这对您不起作用,下一步是查看渲染的源代码并了解发生了什么。如果您发现这不起作用,请建议:
<head>
?