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');
  • First, you should run global $post; get_page_template_slug($post->ID) on the page where you want the javascript and see what the result is. developer.wordpress.org/reference/functions/… – czerspalace Nov 4 '16 at 17:36
  • 1
    You may actually need to add that code to your header or footer, since if you add it in the template, there is always the possibility that a different template is being loaded. – czerspalace Nov 4 '16 at 18:34
  • Do you have post type with slug location? – bravokeyl Nov 5 '16 at 3:22

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;
  • There is no requirement to register first and then en-queue. – bravokeyl Nov 5 '16 at 2:34

Try this putting this code in your function where you enqueue your other scripts and styles and it should work.

wp_register_style( 'template-style', get_stylesheet_directory_uri().'/templates/css/template.css', array(), '', true );

if ( is_page_template( 'template-name.php' ) ) {
    wp_enqueue_style( 'template-style' );

single-location.php naming for a page template is very poor choice as it conflicts with default page template hierarchy single-$posttype.php.

So I would encourage you to change the name to something like tem-location.php, then the following code will work, also please take a note at enqueue handler because that should also not conflict with others(I mean if you already have a script/style enqueued with handle paralar-style then even if you enqueue the script/style with the same handle won't work.

Also please make sure that your template location is correct, for example if you place tem-location.php in page-templates folder then you should check for page-templates\tem-location.php.

function wpse245126_load_theme_files() {
    if (is_page_template('tem-location.php')) {
      wp_enqueue_style('tem-location-style', esc_url( get_template_directory_uri() ).'/paralax.css');
add_action('wp_enqueue_scripts', 'wpse245126_load_theme_files');

You can use this filter, to filter out styles with any condition, or alternate output href string on certain templates, example:

add_filter( 'style_loader_src', function($href){
if(strpos($href, "name-of-allowed.css") !== false) {
return $href;
return false;

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.