Using Conditional Tags

Conditional tags allow you to control the information displayed on a page depending on whether a certain criteria is met. They’re always used in a standard PHP if statement and there are conditional tags for almost every check that you could think of.

The PHP Code

The PHP code used with conditionals is a standard PHP if statement. ie.

if (condition) {
statement here;
}

Using a Conditional Tag

The most used conditional tag I use is the tag to check if the page the user is currently on is the front page. When WordPress is being run as a standard blog (ie. not having a static front page) then the tag is_home() does the job fine. However if you have a static front page set, then this won’t always work. Since WordPress 2.5 a new tag was added – is_front_page(), which does the same job as is_home() but works for both a front page of posts or a static front page.

So, for example, if you wanted to add a welcome message to the front page of your site, then you would edit your index.php theme file (for a blog) or your page.php file (for a static front page and if you have a page.php file) and add in where you want your message to go:

if (is_front_page()) {
echo "<p>Welcome to my website!</p>";
}

Pretty simple!

The Conditional Tags

There are plenty of Conditional Tags available. The more popular ones are:

is_front_page()
Returns true if the current page selected is the front page of the website regardless of whether it’s posts or a static page. Requires WordPress 2.5+
is_single()
Checks if the current page is a single post page. Can accept a parameter of a post ID, post title or post slug to allow you to check for a specific post.
is_page()
Checks if the current page is a static Page. Can accept a parameter of a page ID, page title or page slug, to allow you to check for a specific Page.
is_category()
Checks if the current page selected is a category page. Can accept a parameter of category ID, title or slug.
in_category(’ID’)
Checks if the post is in the category specified by using the Category ID. Can be used on the front page, within the loop to check every page being displayed, or on a single post page. See highlight Featured Posts for an example of use
is_tag()
Checks if the current page is a Tag archive page. Accepts a parameter of the tag slug to check for a specific Tag.
is_author()
Checks if the current page is an author page. Useful for multi author blogs. Can accept a parameter of the Author ID, nickname or nice name to check for a specific author.

Usage

You can use conditional tags in any theme file. In the header they’re useful for improving your front page title. In the loop you can check if it’s a post or Page, in a specific category or more. You can also use them in your sidebar to control what is displayed depending on where you are within the site.

You may wonder, why bother using is_single() or is_category(), when you could just create single.php and category.php template files. Well you could, but why? You’re just duplicating all of the code from the index.php file just to allow you to add in a specific line, or remove a specific line. Then if you decided to change a piece of code that controlled the display of the post you’d have to change all 3 (or more) files, whereas by using conditionals you’d just have the one file to update.

Of course there’s a point at which it would be more suitable to use a separate template file else your index.php file would get so large, covering every possible section of your site, but for one liners or small edits, conditionals are so handy and will save you time in the long run.

Follow this blogger on Twitter!

Sarah Written by Sarah from Stuff By Sarah
Posted on June 29th, 2008 and filed under WordPress Coding & Design
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx

4 Responses to “Using Conditional Tags”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Has is_home() been deprecated in 2.5+

    Hearing about is_front_page for the first time. Is it just a new name to the old function?

    ~Sahil

  2. Sarah says:

    @TechnoDiary.com no is_home() still works, but only works to check if its the front page for posts ie. the standard set up for WordPress. It won’t work if you have a static front page and use is_home(). is_front_page() does both, so it’s a bit of is_home() plus more. Whether it’ll be omitted from future versions who knows.

    @massivemutant.com – glad to hear it :)

  3. Sahil says:

    Ohk I see. Thanks. :)

  4. David Shaw says:

    Nice post Sarah

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Find Out More About Our Newsletter
 

Blogging Tips Sponsors

Blogging Tips Newsletter

 

Blogging Tips Sponsors

 

Latest from the Blogosphere