最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Load JS file only in specific template

programmeradmin2浏览0评论

is_page is working fine as it detects the slug. However, is_page_template function isn't and so I tried global $template too but that's not working either. How can I resolve this issue?

I want to load the the JS file for the page template-parts/content.php

function software_enqueue() {
   if ( is_page( 'software' ) ) {
    wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
   }
   global $template;
    if ( basename( $template ) === 'template-parts/content.php' ) {
      wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
   }
}
add_action( 'wp_enqueue_scripts', 'software_enqueue' );

btw, I tried calling the file (fullwidth.php) too using the code below but that didn't work:

if ( is_page_template('fullwidth.php') ) {
    wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
   } 

fullwidth.php

<?php
/**
* Template Name: Full Width
 */

get_header(); ?>

    <section id="primary" class="content-area content-fullwidth">
        <main id="main" class="site-main" role="main">

            <?php
            while ( have_posts() ) : the_post();

                get_template_part( 'template-parts/content', 'page' );

                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) :
                    comments_template();
                endif;

            endwhile; // End of the loop.
            ?>

        </main><!-- #main -->
    </section><!-- #primary -->

<?php
get_footer();

is_page is working fine as it detects the slug. However, is_page_template function isn't and so I tried global $template too but that's not working either. How can I resolve this issue?

I want to load the the JS file for the page template-parts/content.php

function software_enqueue() {
   if ( is_page( 'software' ) ) {
    wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
   }
   global $template;
    if ( basename( $template ) === 'template-parts/content.php' ) {
      wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
   }
}
add_action( 'wp_enqueue_scripts', 'software_enqueue' );

btw, I tried calling the file (fullwidth.php) too using the code below but that didn't work:

if ( is_page_template('fullwidth.php') ) {
    wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
   } 

fullwidth.php

<?php
/**
* Template Name: Full Width
 */

get_header(); ?>

    <section id="primary" class="content-area content-fullwidth">
        <main id="main" class="site-main" role="main">

            <?php
            while ( have_posts() ) : the_post();

                get_template_part( 'template-parts/content', 'page' );

                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) :
                    comments_template();
                endif;

            endwhile; // End of the loop.
            ?>

        </main><!-- #main -->
    </section><!-- #primary -->

<?php
get_footer();
Share Improve this question asked Nov 22, 2019 at 0:50 Elaine ByeneElaine Byene 1177 bronze badges 4
  • you can enqueue the file in the template file before to call get_header. – Kaperto Commented Nov 22, 2019 at 0:55
  • is_page_template('fullwidth.php') is correct, and should work. The only reason it wouldn't is if fullwidth.php was in a sub-folder, in which case it should be something like templates/fullwidth.php, or if the template wasn't assigned to the page via Page Attributes > Template when editing the page, in which case you'll need to explain how you're actually using this template. – Jacob Peattie Commented Nov 22, 2019 at 4:42
  • fullwidth.php is in the base folder and i'm confused too as to why it's not working! The page was assigned using "Full Width" option which is the template name from the dropdown menu. – Elaine Byene Commented Nov 22, 2019 at 9:15
  • Resolved by putting it directly on the page as Cenay mentioned. – Elaine Byene Commented Nov 22, 2019 at 14:17
Add a comment  | 

1 Answer 1

Reset to default 1

Kaperto is correct, you can enqueue in the actual page template file you want the javascript to appear in. If it would only need to be in a single template file (rather than every page that is fullwidth), I would create a new one which includes the JS, and use that as the template for the page. Ie:

<?php
/** 
* Template Name: Sticky Kit Enabled 
*/ 

wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );

get_header(); ?>

<section id="primary" class="content-area content-fullwidth">
    <main id="main" class="site-main" role="main">

        <?php
        while ( have_posts() ) : the_post();

            get_template_part( 'template-parts/content', 'page' );

            // If comments are open or we have at least one comment, load up the comment template.
            if ( comments_open() || get_comments_number() ) :
                comments_template();
            endif;

        endwhile; // End of the loop.
        ?>

    </main><!-- #main -->
</section><!-- #primary -->
发布评论

评论列表(0)

  1. 暂无评论