1

我正在尝试将脚本排入特定页面模板,但它似乎只是没有在 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 加载主页的脚本很好。我很困扰 :(

4

1 回答 1

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' );
于 2020-02-14T06:08:00.863 回答