Enqueuing style and script based on template not working

71 Views Asked by At

In WordPress, I have a template file called vertical-page.php.

When a page has this template applied, I want to load in a specific stylesheet and script.

I have tried to use is_page_template(), but it doesn't work. When I view the source of the page, the script and styles are not being loaded?

<?php 

add_action('wp_enqueue_scripts', 'theme_scripts');

function theme_scripts() {
    wp_enqueue_style('bootstrap-grid-style', get_template_directory_uri() . '/assets/css/bootstrap-grid.css', array(), STYLE_VERSION);

    if ( is_page_template( 'vertical-page' ) ) {
      wp_enqueue_style('tmp-override', get_template_directory_uri() . '/assets/css/override.css', array(), STYLE_VERSION);
      wp_enqueue_script('overrides-script', get_template_directory_uri() . '/assets/js/main.js', array('jquery'),STYLE_VERSION, true);
    }
    
 ?>
2

There are 2 best solutions below

0
On

If your page template file is something.php and it's location is the main folder of the active theme, you need to pass an argument to the is_page_template() function like this: is_page_template('something.php')

0
On

Try putting the add_action after the function