0

I'm using the following code in functions.php:

if ( !is_page_template( 'page-full-width.php' ) ) {
    wp_enqueue_script( 'flickity-js', get_template_directory_uri() . '/js/flickity.js', array(), filemtime( get_stylesheet_directory() . '/js/flickity.js' ), true );
}

I'm expecting the script not to enqueue if the page template is set to page-full-width.php. When I check on the front-end, the script is still present.

What am I doing wrong here?

2
  • Can you check if the page-full-width.php is in the root folder of the active theme? Commented May 14, 2020 at 10:47
  • Yep, it's in the root of the theme. Commented May 14, 2020 at 10:56

1 Answer 1

0

You need to pass it as a function, or you already have but didn't copy the whole code? If you did not do it, it should look something like this

function page_template_not_enqueue() {
   if ( !is_page_template( 'page-full-width.php' ) ) {
    wp_enqueue_script( 'flickity-js', get_template_directory_uri() . '/js/flickity.js', 
array(), filemtime( get_stylesheet_directory() . '/js/flickity.js' ), true );
}
}

add_action('wp_enqueue_scripts', 'page_template_not_enqueue');

is_page_template function won't work if you have not defined a page as a Wordpress template, you can read up on that here, in short, your page templates need to start with this

<?php
/*
Template Name: Page Full Width
Template Post Type: page
*/
// Page code here...

If this still doesn't work you can try getting the template name with the global $template var, I have not experimented with this second option, I suggest you just make your page template a custom word template with this comment I posted above, but if I was to try and go the other way around I would probably try to do something like this

   function page_template_not_enqueue() {
       global $template

       if ( basename( $template ) === 'page-full-width.php' ) {
        wp_enqueue_script( 'flickity-js', get_template_directory_uri() . '/js/flickity.js', 
    array(), filemtime( get_stylesheet_directory() . '/js/flickity.js' ), true );
    }
    }

    add_action('wp_enqueue_scripts', 'page_template_not_enqueue');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.