0

I can't get the correct css file to load for my page template. The page is correctly loading the template but still using the default css file.

This is the code I'm using to load the page template's css file:

if (is_page_template('page-templates/page-nosidebar.php')) {
        wp_enqueue_style('authorLuncheon-layout-style', get_template_directory_uri() . '/layouts/no-sidebar.css');
    } else {
        wp_enqueue_style('authorLuncheon-layout-style', get_template_directory_uri() . '/layouts/content-sidebar.css');
    }

This is the whole function that contains the code above:

function authorluncheon_scripts() {
wp_enqueue_style( 'authorluncheon-style', get_stylesheet_uri() );

    if (is_page_template('page-templates/page-nosidebar.php')) {
        wp_enqueue_style('authorLuncheon-layout-style', get_template_directory_uri() . '/layouts/no-sidebar.css');
    } else {
        wp_enqueue_style('authorLuncheon-layout-style', get_template_directory_uri() . '/layouts/content-sidebar.css');
    }

wp_enqueue_script( 'authorluncheon-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );

wp_enqueue_script( 'authorluncheon-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );

if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
    wp_enqueue_script( 'comment-reply' );
}
}

Thanks for any help!

1

It seems the problem was it wasn't recognizing it was a page in the first place and thus couldn't recognize if there was a template applied to it.

Changed

if (is_page_template('page-templates/page-nosidebar.php')) {

to

if (is_page() && !is_page_template('page-templates/page-nosidebar.php')) {
0

There are a few problems with your code which I will take you through.

PROBLEM 1

Whether you are using

they all have the first parameter in common, $handle. This is

$handle

(string) (required) Name of the stylesheet (which should be unique as it is used to identify the script in the whole system).

Default: None

As you can see, $handle needs to be unique. You are using authorLuncheon-layout-style twice on two different stylesheets. Just remember though, if you are registering AND enqueueing a style or a script, the $handle parameter for the same script or style must be the same in both functions

SOLUTION TO PROBLEM 1

You should change one of the duplicate $handles

PROBLEM 2

I don't know if you accidentally left it out or just did not add it in you theme, but you should always hook your function containing your script and styles to wp_enqueue_scripts. This is the correct way, and my emphasis, only way to add scripts and styles to the front end of your theme

SOLUTION

Your code should look something like this. BTW, I will change my conditional to check that you are not on this specific page template (!is_page_template('page-templates/page-nosidebar.php'), do something or else do something else, because you are most likely to use this specific page template less that any other template. I have already change the code, so you can just copy and paste it :-)

function authorluncheon_scripts() {
wp_enqueue_style( 'authorluncheon-style', get_stylesheet_uri() );

    if (!is_page_template('page-templates/page-nosidebar.php')) {
        wp_enqueue_style('authorLuncheon-layout-style2', get_template_directory_uri() . '/layouts/content-sidebar.css');
    } else {
        wp_enqueue_style('authorLuncheon-layout-style1', get_template_directory_uri() . '/layouts/no-sidebar.css');
    }

wp_enqueue_script( 'authorluncheon-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );

wp_enqueue_script( 'authorluncheon-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );

if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
    wp_enqueue_script( 'comment-reply' );
}
}

add_action( 'wp_enqueue_scripts', 'authorluncheon_scripts' );
  • 1
    Thank you for the sound advice! Unfortunately I got the same result when using your code, sorry for the delay. – coolpup Aug 6 '14 at 7:34
  • Problem 1 is a non-issue, as the handle only ever gets used once per request, it is more accurate to say that the handle needs to be unique within that request cycle, as it does not persist beyond that. – Keir Lavelle Aug 6 '14 at 8:53
  • True, @KeirLavelle but it is still not recommended that you use the same handle for two different styles/scripts. It is confusing, make it hard to debug and makes it difficult to read later. It might work now, but after a year you make changes and then it becomes a problem – Pieter Goosen Aug 6 '14 at 9:00
  • @coolpup thanks for posting your solution and keeping your question constructive to everyone – Pieter Goosen Aug 6 '14 at 9:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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