Solving The WordPress Template Hierarchy Puzzle: Conditional Tags for Posts and Pages

Josh Pollock - May 08, 2014

http://joshpress.net/blog/category/wordpress/tutorials/solving-the-wordpress-template-hierarchy-puzzle/http://joshpress.net/blog/category/wordpress/tutorials/solving-the-wordpress-template-hierarchy-puzzle/

Last week, as part of my series on the WordPress template hierarchy I covered posts and pages and the difference between front and home pages. Today, I will be covering conditional tags for posts and pages.

If you’ve been following my series you will know which templates you can add to a theme for displaying specific posts or pages, but also that for small changes, I recommend using conditional tags instead. Why add and keep track of a template if you just need to make one change?

Testing For Specific Posts

There are three main functions we use for determining if we are on a single post view, depending on post type–is_single(), is_singular() and is_page().

is_single() vs is_page()

The first one is_single() will return true for any post type, including custom post types, but not pages or attachments.

Without any arguments, is_single() returns when any single post is being displayed. It can also be used to test for a specific post by passing a post ID, slug or post titles. For example, is_single( 7 ) will only return true if the single post with the id of 7 is currently being displayed. You can also use an array, mixing IDS, slugs and post titles. This means that is_single( array( 7, 5, 'The Jedi', 'the-sith' ) ) would return true for posts with the ID of 7 or 5 or a post with the title “The Jedi” or the slug “the-sith”.

The function is_single() does not work with pages. To test if a page is being displayed or for a specific page, you can use is_page() instead. is_page(). You can test for specific pages by ID, slug or post title with is_page() just like with is_single().

Using is_singular()

While is_single() only works for certain post types, is_singular() works for all post types, including posts, custom post types, pages and attachments. Keep in mind that it works a little differently than is_single(). The argument for is_singular() is not ID, post, or slug. Instead it is a post type.

This means that while is_singular() will return true for any single post type view, is_singular( 'jedi' ) will return true for a single post of the “jedi” post type, not a post with the slug or post title of “jedi”. If you want to test if the current post is a single post of a specific ID and is of a specific post type, your conditional would look like this:

if ( is_single( 11358 ) && is_singluar( 'jedi ) ) {
   //true if showing single post view of post ID #11358 in the custom post type "jedi"
}

Home and Front Page

In my post on front and home page templates I explained the difference between what a home and front page template is. The same rules apply to the functions is_home() and is_front_page(). If you are showing a static front page, and do not have a “front_page.php” template, “page.php” will be used instead, or “index.php” if no “page.php” exists. You can make adjustments specific to the static front page by wrapping them in is_front_page().

If your theme doesn’t have a “home.php” template, you can make changes to “index.php” that will only apply when “index.php” is being used to display the main blog index, not when it’s being used as the fallback for any other view, by wrapping the changes in is_home().

HelpFul Links