Ed: This article is part of the 21 Days of WordPress Tips
The WordPress core provides several “conditional tags” that are helpful in showing different content or code depending on where you are on the site. You can read about all of them at the WordPress codex, however I’m going to share with you a few I use the most plus a couple things to look out for.
In most business websites you’re going to hard code the top navigation as opposed to automatically showing links to all the top level pages. The problem this can cause is when you want to indicate to the user what section of the site they are on. This is where the is_page() conditional tag comes in. You can use it to test whether the user is on the page and then apply a different CSS class to the element. Here’s an example:
<li><a <?php if(is_page(2)) : ?> class="current_page"
<?php endif; ?>href="<?php bloginfo('url'); ?>/about-us/">
As you can see, I’m using is_page(2) to see if the current page has an ID of 2. If it does, I will add a class of “current_page” to the element which will allow me to style it differently.
If you read through the WordPress codex entry on is_page(), you’ll notice I could have also used:
The page’s slug: is_page(‘about-us’)
Or the page’s title: is_page(‘About Us’)
And while this is much easier to use and produces more readable code, if the client ever changes the name of the page then this code will break. I highly recommend using the page ID instead of the slug or title when using is_page().
is_home() and is_front_page()
On most sites, the home page has a different display with different elements that the rest of the site. This is where these two conditional tags can be very helpful.
However, there are a couple things to look out for…
At first glance you might assume these two conditional tags do the same thing. Isn’t your home page the same as your front page?
This is not how WordPress views it.
The is_home() conditional tag is left over from the earlier days of WordPress when it was merely a blog platform. As WordPress grew into a full content management system and added the ability to set a static page as your home page (In the admin panel: Settings -> Reading -> Front page display), is_home() no longer did what it’s name implies.
The is_home() conditional tag will only return true on the main blog page of your site.
If you set a static page as your front page, is_home() will return false when viewing your home page.
This is where I recommend you use is_front_page(). It will return true when you are looking at the home page of your site whether it’s set to a static page or your main blog page. It’s more consistent and, therefore, more useful.
This is another conditional tag that I use a lot. It returns true whenever you are viewing a single blog post. This is great for showing different sidebar elements such as related posts or author information for each blog post.
Another feature that is often requested is to treat a post differently depending on what category it is in.
For instance, maybe you want to show the contact information for the support department on any post addressing support issues. You can use the in_category() conditional tag to do this automatically so you don’t have to remember to type it in every time.
There are several other conditional tags available, but these are the ones that you’ll find the most use for.
January 7, 2009