I’m trying to enque stylesheet depending on template but unfortunetelly my code isn’t working. What I am doing wrong?

 if ( is_page_template('single-location.php')) {
       function themename_include_page_specific_css() {
  wp_enqueue_style('paralax_style', get_template_directory_uri().'/paralax.css');
}
add_action('wp_enqueue_scripts', 'paralax_style');
    }

I tried also with that one, but still nothing.

 function load_theme_files() {
    if (is_page_template('single-location.php')) {
      wp_enqueue_style('paralax_style', esc_url( get_template_directory_uri() ).'/paralax.css');
    }
}
add_action('wp_enqueue_scripts', 'load_theme_files');

4 Answers
4

You need to first register the style and then en-queue it! This might work!

function custom_style_method() {
    wp_register_style( 'paralax_style', get_template_directory_uri().'/paralax.css' );
}
add_action('wp_enqueue_scripts', 'custom_style_method');

add_filter( 'template_include', 'themename_include_page_specific_css', 1000 );

function themename_include_page_specific_css( $template ){

    if ( is_page_template('single-location.php' ) ) {
            wp_enqueue_style( 'paralax_style' );
    }

return $template;
}

Leave a Reply

Your email address will not be published. Required fields are marked *