0

I need to develope a structure where I use all the conditionals for every type of page, post and archive.

On WordPress we have these pages (extracted from a template hierarchy):

  • Error 404 Page
  • Search Result Page
  • Archive Page
    • Custom Taxonomy
    • Category Archive
    • Tag Archive
    • Author Archive
    • Date Archive
      • Year Archive
      • Month Archive
      • Day Archive
    • Custom Post-Type Archive
  • Singular Page
    • Single Post Page
      • Attachment Post
      • Custom Post
      • Blog Post
    • Static Page
      • Page Template
  • Site Front Page
  • Blog Posts Index Page
  • Comments Popup Page

According to these tree, if I have a WordPress theme with all these pages, I need a structure to avoid bugs, knowing that it's possible to change Blog Posts Index Page for a Site Front Page, and using an another Static Page as it was the Blog Index/Archive.

is_home()

↑ This is only TRUE when the Blog Index is shown in the Front Page.

is_front_page()

↑ This conditional returns TRUE when it is the Blog Index or a Static Page .

is_front_page() and is_home()

↑ There is no conditional tag for the blog page. So this code helps to detect this page. The right order to avoid bugs is this:

if ( is_front_page() && is_home() ) {
    // Default homepage
} elseif ( is_front_page() ) {
    // static homepage
} elseif ( is_home() ) {
    // blog page
} else {
    // everything else
}

Then, let's see how it would be:

if ( is_front_page() && is_home() ) {
    //...
} elseif ( is_front_page() ) {
    //...
} elseif ( is_home() ) {
    //--------------------------------
} elseif ( is_post_type_arachive() ) {
    //...
} elseif ( is_tax() ) {
    //...
} elseif ( is_author() ) {
    //...
} elseif ( is_tag() ) {
    //...
} elseif ( is_category() ) {
    //...
} elseif ( is_year() ) {
    //...
} elseif ( is_month() ) {
    //...
} elseif ( is_day() ) {
    //...
} elseif ( is_date() ) {
    //...
} elseif ( is_archive() ) {
    //... Else: any archive page
    //--------------------------------
} elseif ( is_attachment() ) {
    //...
} elseif ( is_single() ) {
    //--------------------------------
} elseif ( is_page_template() ) {
    //...
} elseif ( is_page() ) {
    //--------------------------------
} elseif ( is_singular() ) {
    //... Else: any singular page
    //--------------------------------
} elseif ( is_404() ) {
    //...
} elseif ( is_search() ) {
    //...
} elseif ( is_paged() ) {
    //... Additional settings
    //... for paged archives.
}

I want to use it for several features: breadcrumbs, meta information (Open Graph, meta tags), or simply to customize the titles.

Is it the right order?

Should I use is_main_query() at some point?

1

There are no real correct order in which to use the conditionals. A general rule of thumb is to put the most used condition first and the least used last

I also think that a switch will be a bit better here as it will be bit faster. Normal else/if statements re-evalutes the statement before executing, where as switches doesn't re-evalutes the conditions.

Just a note, is_archive() will return true on all archives, so be careful with that. On the point on frontpages and blogpages, you can go and check this recent post I have done

The conditional is_main_query() is not needed. It is basically just needed where something can influence the main query and custom queries like pre_get_posts

EDIT

A basic switch normally evaluates to true. If a condition evaluates to true, execution of the switch stops and the value is output from the switch. Here is a basic switch

switch ( true ) {

case (is_home() ):

    $text = 'This is home';

    break;

case ( is_category() ):

    $text = 'This is a category page';

    break;

case ( is_tag() ):

    $text = 'This is a tag page';

    break;

default:

    $text = 'This is not home, category or tag pages';

    break;

}

You can now output $text in your header like

echo $text;

This is just something simple and should give you a basic idea

  • Let me ask one thing related to it: in order to initialize the variable of the switch statement, what value should I use in that case? I can't find any example nor idea. – Gerard Jan 22 '15 at 0:56
  • I'm using $selectpage = get_post_type( get_the_ID() ); and it's working at the moment. Then, case ( is_front_page() ): and so on. – Gerard Jan 22 '15 at 1:12
  • 1
    Good morning, please see my edit. Hope is helps :-) – Pieter Goosen Jan 22 '15 at 4:21
  • It was so easy and I was complicating it. It works perfect, now! Thank you! – Gerard Jan 22 '15 at 4:37
  • My pleasure. Enjoy :-) – Pieter Goosen Jan 22 '15 at 4:45

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.