There's no place like is_home : WordPress conditional tags

Conditional tags are used in WordPress to change content according to what sort of page is being displayed. is_home() will test if the page is your blog's home page; is_single() tests if it's a single post page.

Why do this when the WordPress template hierarchy tests for these things anyway? Most obviously, conditional tags help to cut down the number of theme files you need: if there are only tiny differences between - say - your home page and your archives pages, you can use the same file for both and use conditional tags to deal with the small differences needed.

For example:

  • you might use the same file for the home page, archive pages and search results pages, but use conditional tags to label "Archive for [date/category/tag/author]" and "Search results for [search term]".
  • If you were running a minimalist theme with just an index.php, you might show the_excerpt() on all pages except single.php which would have the full post content,
  • or you might choose to show your blog's sidebar on the home page and single post pages, but not on search and archive pages,
  • you might serve up a different stylesheet depending on the type of page,
  • or you might make the logo a link to the home page on every page but the home page itself.

This is how the tags work:<?php if (is_single()) {
// do something
}
else {
// do something else
}
?>

Pretty straightforward, huh? If you want to test for more than one thing, that goes like this:

<?php if (is_search() || is_archive()) {
// do something
}
?>

List of conditional tags

Here are some of the more commonly used conditional tags: there's a full list in the Codex.

  • is_home() - if it's the blog's home page (but see below: if the blog has a static page for the front page, is_home() is the most recent posts page)
  • is_front_page() - if the setting chosen under Settings > Reading > Front page displays is showing: this works if your front page is a static page rather than a list of blog posts
  • is_admin() - if in the admin section
  • is_single() - if a single post page
  • is_archive() - if any kind of archive
  • is_date() - if a date-based archive being shown. Also available in is_year(), is_month(), is_day() and is_time()
  • is_author() - if an author archive
  • is_category() - if a category archive
  • is_tag() - if a tag archive
  • is_page()
  • is_search() - if a search results page
  • is_404()

Testing for specifics

Many of the above tags can also be used to look for specific posts, categories, tags etc. For example, is_category('pictures of my cat') or is_tag('cute').

Templates versus conditional tags

So when should you use conditional tags, and when should you have a whole new template file? Everyone has their own preferences, but I'd say it's about the extent of the difference. If it's just a few lines - a title, a stylesheet, the inclusion or not of another file - conditional tags work nicely. If it's any more different than that, separate templates will be easier - especially six months down the line when the theme is less fresh in your mind.

Share this post:
  • email
  • del.icio.us
  • Facebook
  • FriendFeed
  • LinkedIn
  • StumbleUpon
  • Tumblr
  • Twitter

Tags:

Posted by Sue on August 11, 2009 in Themes, WordPress.

Tweetbacks

  1. BlogMum post: There's no place like is_home : WordPress conditional tags http://blogmum.com/?p=1142

Leave a Reply