为什么这不起作用?我试图有选择地加载脚本,但第二条if语句没有在应该加载的地方加载脚本。
function my_init() {
if (!is_admin()) {
wp_deregister_script(\'jquery\');
wp_register_script(\'jquery\', \'https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js\', false, \'1.5\');
wp_enqueue_script(\'jquery\');
if (in_category(\'41\') ) {
wp_register_script(\'validator\', \'/wp-content/themes/myTheme/library/jquery.validationEngine.js\', array(\'jquery\'), \'2.0\', false);
wp_enqueue_script(\'validator\');
wp_register_script(\'vEngine\', \'/wp-content/themes/myTheme/library/jquery.validationEngine-en.js\', array(\'validator\'), \'2.0\', false);
wp_enqueue_script(\'vEngine\');
}
}
}
add_action(\'init\', \'my_init\');
如果我删除了该类别的第二条If语句,它会起作用,但我想将其限制为一个类别,甚至是一个is\\u single(\'123\')
我做错了什么?
SO网友:EAMann
我翻了翻,似乎抄本引用中可以使用条件标记,如is_category()
在init
挂钩不正确。但是,此代码确实可以正常工作:
function my_init() {
if (!is_admin()) {
wp_deregister_script(\'jquery\');
wp_register_script(\'jquery\', \'https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js\', false, \'1.5\');
wp_enqueue_script(\'jquery\');
}
}
function my_scripts() {
if (in_category(\'41\') ) {
wp_register_script(\'validator\', \'/wp-content/themes/myTheme/library/jquery.validationEngine.js\', array(\'jquery\'), \'2.0\', false);
wp_enqueue_script(\'validator\');
wp_register_script(\'vEngine\', \'/wp-content/themes/myTheme/library/jquery.validationEngine-en.js\', array(\'validator\'), \'2.0\', false);
wp_enqueue_script(\'vEngine\');
}
}
add_action(\'init\', \'my_init\');
add_action(\'wp_print_scripts\', \'my_scripts\');