我试图将脚本排入特定的页面模板,但似乎只是没有加载if语句中的任何脚本,我已经尝试了is_page版本并且它做了同样的事情。如果有人能够指出我出错的地方,我将非常感激。
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' );
通过is_front_page正常加载主页的脚本是多么奇怪。我很困惑:(
答案 0 :(得分:0)
下面的代码可以完美工作。
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' );