0

I have three templates I want to be able to use (Index Page, Main Template, and Default). I was able to add a "if/else" statement into a function to get the correct CSS file to load depending on which template was being used between two templates (Index and Default).

However, when I try to use "elseif" so I can add in another option for Main Template, it doesn't work. Instead the page that uses Main Template loads the Index Page CSS.

This is the code in question:

    if ( is_page() && ! is_page_template( 'page-templates/index_page.php' ) ) {
        wp_enqueue_style( 'authorLuncheon-layout-style1', get_template_directory_uri().'/layouts/index_page.css' );
    } elseif ( is_page() && ! is_page_template( 'page-templates/main_template.php' ) ) {
        wp_enqueue_style( 'authorLuncheon-layout-style2', get_template_directory_uri().'/layouts/main_template.css' );
    } else {
        wp_enqueue_style( 'authorLuncheon-layout-style3', get_template_directory_uri().'/layouts/content-sidebar.css' );
    }

Here's the whole function:

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

    if ( is_page() && ! is_page_template('page-templates/index_page.php' ) ) {
        wp_enqueue_style( 'authorLuncheon-layout-style1', get_template_directory_uri().'/layouts/index_page.css' );
    } elseif ( is_page() && ! is_page_template( 'page-templates/main_template.php' ) ) {
        wp_enqueue_style( 'authorLuncheon-layout-style2', get_template_directory_uri().'/layouts/main_template.css' );
    } else {
        wp_enqueue_style( 'authorLuncheon-layout-style3', 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' );
    }
}
add_action( 'wp_enqueue_scripts', 'authorluncheon_scripts' );
1

I think your big problem here is your use of underscores (_) in your page template names. According to the Handbook of Coding Standards

Files should be named descriptively using lowercase letters. Hyphens should separate words.

  • my-plugin-name.php

Also, for best practices you should call your custom page templates page-whatever.php

So for instance, index_page.php should be renamed to page-index.php and index_page.css should be renamed to something like index-style.css

NOTE: Just one think to remember, if you are talking about your homepage which uses index.php or a blog page that uses index.php or home.php, you should use is_home() instead of is_page() && !is_page_template('page-templates/index_page.php'). If you have set a static front page, you should use is_front_page() instead of is_page() && !is_page_template('page-templates/index_page.php'). Your front page should also be named front-page.php for good practice, although you can use other names as set out here

I would think that this will solve your problem. If not, let me know

EDIT

 if (is_page() && !is_page_template('page-templates/index_page.php')) {
        wp_enqueue_style('authorLuncheon-layout-style1', get_template_directory_uri() . '/layouts/index_page.css');
    } elseif (is_page() && !is_page_template('page-templates/main_template.php')) {
        wp_enqueue_style('authorLuncheon-layout-style2', get_template_directory_uri() . '/layouts/main_template.css');
    } else {
        wp_enqueue_style('authorLuncheon-layout-style3', get_template_directory_uri() . '/layouts/content-sidebar.css');
    }

Read your code above in normal English

if( a page is displayed and the template is NOT page-templates/index_page.php) {

enqueue my style authorLuncheon-layout-style1

} else if( a page is displayed and the template is NOT page-templates/main_template.php) {

enqueue my style authorLuncheon-layout-style2

} else {

when the template displayed IS page-templates/main_template.php or page-templates/index_page.php {

enqueue my style authorLuncheon-layout-style3

}

So this is basically a wrong use of operators. Remember ! means the opposite of the conditional it is attached to. So you should rearrange your code completely for this to work. This is just a rough draught

if ( is_page() && is_page_template( 'page-templates/index_page.php' ) ) {
        //enqueue whatever style you want to use ON THIS SPECIFIC PAGE 
    } elseif ( is_page() && is_page_template( 'page-templates/main_template.php' ) ) {
       //enqueue whatever style you want to use ON THIS SPECIFIC PAGE
    } else {
       //enqueue whatever style you want to use ON ANY OTHER PAGE
    }

EDIT 2

You can also try this. I would also not use the is_page()

if ( !is_page_template( 'page-templates/index_page.php' ) && !is_page_template( 'page-templates/main_template.php' ) ) {
       //enqueue whatever style you want to use ON ANY OTHER PAGE THAN THIS TWO
    } elseif ( is_page_template( 'page-templates/index_page.php' ) ) {
        //enqueue whatever style you want to use ON THIS SPECIFIC PAGE 
    } elseif ( is_page_template( 'page-templates/main_template.php' ) ) {
       //enqueue whatever style you want to use ON THIS SPECIFIC PAGE
    }
  • I renamed the files but unfortunately nothing changed – coolpup Aug 6 '14 at 9:57
  • I'm quickly busy with a long edit on my answer. Realized something else – Pieter Goosen Aug 6 '14 at 9:58
  • I understand that it shouldn't work the way I have it set up with the "!", and originally I had it set up like you suggested in your edit, but for some reason it breaks and changes everything to the default CSS file (the third option) instead of the correct ones. I tried it again just to make sure, with the newly renamed files, and I get the same result – coolpup Aug 6 '14 at 10:14
  • What happens if you remove is_page() && – Pieter Goosen Aug 6 '14 at 10:18
  • I tried edit 2, both with and without is_page() &&, but it said "Warning: Division by zero" and referred to the first line of your edit. The page loaded below the warning error, but showed no change from before – coolpup Aug 6 '14 at 10:41

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.