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();
  • you can enqueue the file in the template file before to call get_header. – Kaperto Nov 22 '19 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 Nov 22 '19 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 Nov 22 '19 at 9:15
  • Resolved by putting it directly on the page as Cenay mentioned. – Elaine Byene Nov 22 '19 at 14:17
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 -->
  • I'll give this method a go. Thanks. – Elaine Byene Nov 22 '19 at 9:15
  • Thank you. It's resolved. I'm wondering why my original code isn't though! – Elaine Byene Nov 22 '19 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.