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 used with conditionals is a standard PHP if statement. ie.
if (condition) {
statement here;
}
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!
There are plenty of Conditional Tags available. The more popular ones are:
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.
Author comments are in a darker gray color for you to easily identify the posts author in the comments
Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums
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
@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
Ohk I see. Thanks.
Nice post Sarah