首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Wordpress入队脚本加入页面模板失败

将Wordpress入队脚本加入页面模板失败
EN

Stack Overflow用户
提问于 2012-10-23 20:15:51
回答 1查看 2K关注 0票数 1

我试图将一个脚本放入一个特定的页面模板中,但它似乎没有加载if语句中的任何脚本,我尝试过is_page版本,它也做了同样的事情。如果有人能指出我的错误所在,我将不胜感激。

代码语言:javascript
复制
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 fine为主页加载的脚本。我很困惑:

EN

回答 1

Stack Overflow用户

发布于 2020-02-14 14:08:01

下面这段代码可以完美地工作。

代码语言: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' );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13030221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档