Conditional Tags

WP function Returns true if:
is_404() The request returns an “HTTP 404: Not Found” error.
is_admin() The request is for the admin Dashboard or the administration panel.
is_archive() The request is for any type of archive page. An archive can be a Category, Tag, Author, Date, Custom Post Type or Custom Taxonomy based pages.
is_attachment() An attachment. An image or other file uploaded through the post editor’s upload utility. Attachments can be displayed on their own ‘page’ or template.
is_author() An author archive page.
is_category() A category archive page.
is_date() A year, month, or day
is_day() A day
is_feed() A syndication feed
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_home() The request is for the blog homepage. 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_month() A month
is_network_admin() The request is for a network admin screen (wp-admin/network).
is_page() The request is for an existing single page.
is_post_type_archive() The request is for an archive page of a given post type(s).
is_search() The request is for the search result page.
is_single() A single post. Works for any post type, except attachments and pages.
is_sticky() The current post is a Sticky Post meaning the “Stick this post to the front page” check box has been checked for the post.
is_tag() The request is for a Tag archive page.
is_tax() The request is for a custom taxonomy archive page.
is_time() An hour, minute, or second
is_year() A year

Routing with Conditional Tags

  • When developing an extendable theme it’s best to stick to the Template Hierarchy.
  • If the theme is not extendable, the Template Hierarchy can be bypassed with a custom theme router in the index.php file which is more organized and centralized.
  • The WordPress function get_template_part() can be used to include template files from custom sub-directories making it easier to organize the theme files.
    An example of theme routing using conditional tags in the index.php file.
  • NOTE: When you use is_home() and is_front_page(), you have to use them in the right order to avoid bugs and to test every user configuration.
// wp-content/themes/{my_theme}/index.php

// Route the request to it's template file.
    if (is_front_page() && is_home()) { get_template_part('partials/default_home'); }
elseif (is_front_page()) { get_template_part('partials/static_home'); }
elseif (is_home())       { get_template_part('partials/blog_page_home'); }
elseif (is_single())     { get_template_part('partials/single'); }
elseif (is_search())     { get_template_part('partials/search'); }
  else                   { get_template_part('partials/404'); } // By default everything is a 404 page.
// wp-content/themes/{my_theme}/partials/index.php

<?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'); ?>