Javascript Wordpress将脚本排入页面模板失败
我试图将一个脚本排队到一个特定的页面模板中,但它似乎没有加载if语句中的任何脚本,我尝试了is_页面版本,它也做了同样的事情。如果有人能指出我错在哪里,我将非常感激Javascript Wordpress将脚本排入页面模板失败,javascript,wordpress,Javascript,Wordpress,我试图将一个脚本排队到一个特定的页面模板中,但它似乎没有加载if语句中的任何脚本,我尝试了is_页面版本,它也做了同样的事情。如果有人能指出我错在哪里,我将非常感激 function di_load_javascript() { // Deregister the included library wp_deregister_script( 'jquery' ); // Register the library again from Google's CD
function di_load_javascript()
{
// Deregister the included library
wp_deregister_script( 'jquery' );
// Register the library again from Google's CDN
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js', array(), null, false );
wp_register_script( 'jquery-masonry', get_template_directory_uri() . '/js/jquery.masonry.min.js', array( 'jquery' ), null, true );
wp_register_script( 'blog-scripts', get_template_directory_uri() . '/js/blog.js', array( 'jquery' ), null, true );
wp_register_script( 'home-scripts', get_template_directory_uri() . '/js/home.js', array( 'jquery' ), null, true );
// Enqueue the scripts for all pages
wp_enqueue_script( 'jquery' );
// Enqueue the scripts for Home
if ( is_front_page() ) {
wp_enqueue_script('home-scripts');
};
// Enqueue the scripts for Blog (is_page_template fails for some reason)
//if ( is_page(array(2,'blog','Blog')) ) {
if ( is_page_template('templates/blog.php') ) {
wp_enqueue_script('jquery-masonry');
wp_enqueue_script('blog-scripts');
};
}
add_action( 'wp_enqueue_scripts', 'di_load_javascript' );
奇怪的是,通过“首页”加载主页的脚本很好。我很困惑:(下面的代码工作得很好
function my_enqueue_stuff() {
// "page-templates/about.php" is the path of the template file. If your template file is in Theme's root folder, then use it as "about.php".
if(is_page_template( 'page-templates/about.php' ))
{
wp_enqueue_script( 'lightgallery-js', get_template_directory_uri() . '/js/lightgallery-all.min.js');
wp_enqueue_script('raventours-picturefill', "https://cdn.jsdelivr.net/picturefill/2.3.1/picturefill.min.js", true, null);
}
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );
您是否包括
wp_head()
在你的header.php中?是的,它将jquery排队,如果我删除if语句,所有脚本都将加载你的wordpress版本是3.4?子目录只在3.4+中工作,它将主脚本和jquery排队,这就是你要说的吗?因为它们与其他脚本不同的是,其他脚本将通过页脚(第五个参数为true)。您正在调用wp_footer吗?如果您使用的是PHP版本>5.3,您可以执行添加_操作('wp_enqueue_scripts',function(){echo'Template:'。获取_page_Template_slug(获取_queryed_object_id());以查看_page_模板的具体功能。