Conditional Tags

Conditional Tags are WordPress functions which test to see whether a certain condition is met by returning either TRUE or FALSE. Depending on what conditions are met a developer can define what to show and or what template files to include into a page.

Tag True
is_home() When the main blog home page should be displayed. This is the page which shows the time based blog content of your site, so if you’ve set a static Page for the Front Page (see below), then this will only be true on the Page which you set as the “Posts page” in Administration > Settings > Reading.
is_front_page() When the front of the site should be displayed, whether it is posts or a Page. Returns true when the main blog page is being displayed and the ‘Settings > Reading ->Front page displays’ is set to “Your latest posts”, or when ‘Settings > Reading ->Front page displays’ is set to “A static page” and the “Front Page” value is the current Page being displayed.
is_single() When a single post of any post type (except attachment and page post types) is being displayed. This function can get specific. See the docs.
is_page() When any Page (a post with post_type ‘page’) should be displayed.
is_category() A category
is_author() An author
is_date() A year, month, or day
is_year() A year
is_month() A month
is_day() A day
is_time() An hour, minute, or second
is_archive() A category, author, or date
is_search() A search
is_404() A 404 page
is_feed() A syndication feed

Routing with Conditional Tags

WordPress intendeds for you to work with the Template Hierarchy, which is wise if you are working on an extendable theme, but another way of routing template files is to forgo the use of all direct template files and build a theme router which is more organized and centralized.

The WordPress function get_template_part() can be used to include template files from sub-directories making it easier to organize the theme files.
An example of theme routing using conditional tags in the index.php file:

// Route the request to it's template file.
    if (is_home())   { get_template_part('pages/home'); }
elseif (is_single()) { get_template_part('pages/single'); }
elseif (is_search()) { get_template_part('pages/search'); }
  else               { get_template_part('pages/404'); } // By default everything is a 404 page.

The pages/404.php template file:

<?php get_template_part('partials/doc_head'); ?>
<h1>404 Page Not Found</h1>
<p>Sorry but there is nothing here.</p>
<?php get_template_part('partials/doc_foot'); ?>