将多个 js 文件包含到 WordPress 主题中的最佳方法是什么?
我正在尝试将许多 js 文件添加到我新创建的主题中(我是 WordPress 主题的新手) 我试图以这种方式做到这一点:
function includejQuery() {
if (!is_admin()) {
wp_deregister_script('jquery');
$jqueryPath = get_bloginfo('template_directory') . '/js/jquery-1.6.2.min.js';
wp_register_script('jquery', $jqueryPath);
wp_enqueue_script('jquery');
}
}
function includeddAccordionUI() {
if (!is_admin()) {
wp_deregister_script('ddaccordion');
$ddaccordionPath = get_bloginfo('template_directory') . '/js/ddaccordionUI.js';
wp_register_script('ddaccordion', $ddaccordionPath , array("jquery"));
wp_enqueue_script('ddaccordion');
}
}
add_action('init', 'includejQuery');
add_action('init', 'includeddAccordionUI');
上述想法可以很好地包含一个文件,但如果你包含 2 个文件,php 将进入无限循环(或者页面不会停止加载)
是否有更好的方法来包含很多js文件?
I am trying to add many js files to my newly created theme (I am new to wordpress theming )
I was trying to do it in this way :
function includejQuery() {
if (!is_admin()) {
wp_deregister_script('jquery');
$jqueryPath = get_bloginfo('template_directory') . '/js/jquery-1.6.2.min.js';
wp_register_script('jquery', $jqueryPath);
wp_enqueue_script('jquery');
}
}
function includeddAccordionUI() {
if (!is_admin()) {
wp_deregister_script('ddaccordion');
$ddaccordionPath = get_bloginfo('template_directory') . '/js/ddaccordionUI.js';
wp_register_script('ddaccordion', $ddaccordionPath , array("jquery"));
wp_enqueue_script('ddaccordion');
}
}
add_action('init', 'includejQuery');
add_action('init', 'includeddAccordionUI');
the above mentioned idea work just fine to include one file but if you include 2 file php will go into infinite loop (or the page won't stop loading )
is there any better way to include many js files ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将脚本前端排队时使用操作
wp_enqueue_scripts
。这样您就不必检查您是否处于管理员状态。当您确实想在管理中加载它时,请使用admin_enqueue_scripts
。这不仅是为了摆脱你的bug,也是为了摆脱条件。也只使用一个函数并从该函数加载所有js,以防止可能的重复代码(例如您当前的条件)。现在,如果这不起作用,则与 Wordpress 无关,可能是由于 js 所致。最后的建议是使用包含的 js 文件,通常就足够了!wp_enqueue_script 的法典页面:http://codex.wordpress.org/Function_Reference/wp_enqueue_script
Use the action
wp_enqueue_scripts
when enqueuing scripts front end. This way you don't have to check if you're in admin. Useadmin_enqueue_scripts
when you actually want to load it in admin. This is not only to get rid of your bug, but also to get rid of the conditional. Also use only one function and load all js from that function, to prevent possible duplication of code (such as your current conditional). Now if this does not work, it has nothing to do with Wordpress and probably is due to the js. A last recommendation is to use the included js files, the usually suffice!Codex page for wp_enqueue_script: http://codex.wordpress.org/Function_Reference/wp_enqueue_script