The WordPress conditional tags are some of the most powerful tools a theme designer can master. As WordPress themes get more complicated and more CMS like, everyone will eventually run into the need for conditionals. In my previous post I showed you how to write less code using conditional tags and the WordPress hierarchy. Quite a few things have changed since that post, and this time I’m going to show you some more advanced ways to use conditional tags.
What’s New?
The WordPress dev team heard the cries of the masses and has added new conditional tags to the core. A few include:
<? is_front_page() is_single(array(17,'beef-stew','Irish Stew')) is_page(array(42,'about-me','About Me And Joe')) is_page_template() ?>
Head on over to the codex conditionals page for a full list. The most important new tags are the ones which allow you to use arrays. In the past you may have had to use complex code to achieve the same result. The ability to filter content by several pages/categories/tags is incredibly useful especially in building large websites. The Codex does a good job at explaining the new tags so I won’t go into that here.
How’s Your PHP?
If you theme WordPress you probably know at least some basic PHP. The most basic and useful PHP you can learn are comparison operators. The operators will return true or false and work the same way as the WordPress conditional tags.
Let’s say you wanted some text to only show on the home page(ID is 42) and your about page. One way to do this is:
<?
if ( is_page( array (42,'About') ) ){
echo "Hello World";
}
Lets say you didn’t know the ID of the page or the name, maybe it is in a theme and you want the user to set where the text appears. In this example $text_pages is a list set somewhere either by the user or some other function.
<?
$text_pages = get_option('pages');
if ( is_page( array($text_pages) ) ){
echo "Hello World";
}
More complex examples
OK so far pretty basic stuff. Lets see something more complicated. We have text we want to show on a static home page, some posts in a certain category, but not in the search pages. Using PHP comparison operators and WordPress conditionals this is easy.
<?
if ( (is_front_page() || in_category('3') && !( is_search() ) ) {
echo "Hello World";
}
Reading this in English goes like so: If is a static home page, or(||) in category 3 and(&&) is not(!) a search page. The exclamation mark is a shortcut for does not equal(!=).
As you can see developing complex conditionals can be very powerful, and can also save you from writing a bunch of repeated code. Lets take it even further and use some variables now.
Lets say you want the user to choose if there is a feature post displayed. You also want them to be able to set the categories used for a feature post. You also want don’t want the feature story repeating itself while paging. To accomplish this we’re going to use a combination of variables, WordPress conditionals, and some if else statements.
<?
$feature = get_option('feature'); // Gets the option to display feature post.
if( ($feature == 'Yes') && !(is_paged()) ) { //Excludes the feature post when paging through posts
$feature_cats = get_option('feature_cats'); // Gets list of feature categories
query_posts(array('category__in' => array($feature_cats)));
//The loop and post formatting
}
Making Your Own Conditionals
If you really want to get dirty, you can form your own conditionals. The Codex hints at this ability with this example:
!empty($post->post_excerpt)
Pretty simple right? If the post excerpt is empty this will return false. Likewise removing the ! will then return false for anything that has an excerpt.
With a basic understanding of if else statements, and a not so basic understanding of WordPress functions you could create a conditional statement for just about anything granted you had something to compare to get a true or false statement.
Lets come up with another example. Say you want to display a special title whenever a post has approved comments.
<?php $foo = get_approved_comments($post->ID);
if ( !empty($foo) ) {
echo "<h1>I Gots Comments!</h1>";
} ?>
Quick Comparison Operators Overview
The php comparison operators article goes into more detail but I’ll give you quick overview from a WordPress point of view.
When using these operators with WordPress conditional tags and if else statements keep track of your parenthesis! I’ve strung together quite a long string which ended in an equally long string of parenthesis, which will be a headache to decode later.
There are a few operators that are used more often. These are: equal( == ), not equal ( != ), or( || ), and( && ). Using these four you can construct some heavy conditionals.
Grouping is important! Just like math, you can group operators and conditionals to get different true or false statements. ex. (in_category(42) && !(is_search()) || is_archive())
Use !(), !empty(), empty() to achieve some more complex conditionals and create your own conditional statements.
The End
So there are some examples of complex WordPress conditionals, comparison operators, booleans, if elses, and some pie(update: I ate all the pie). This isn’t a complete list of what is possible, infact I don’t think it even scratches the surface. Conditionals are great, and are even more powerful than ever.
13 Comments
Leave a CommentFantastic, great tips and explanations
19th Oct 2008
I would like different categories to show in the nav bar on different pages. Can this be done? I want certain categories to show on my home page. On page 2 I want categories that are different than the home page. On page 3 I want categories that are different than the home page and page two.
If this can be done, can you tell me how or where I can learn this. Maybe an example? I seem to be stuck on this topic and need a kickstart. Any help would be appreciated.
Thanks
4th Nov 2008
vb,
Do you mean while paging through posts or on individual wordpress pages(like About, Home, Contact)
It may be possible while paging through posts, but I know for sure it is possible if your are talking about individual pages.
4th Nov 2008
I would have pages listed across the nav bar on the home page with categories listed in the sub nav bar.
On page two I would like different categories to be displayed in the sub nav bar.
On a third page I would like different categories dislayed in the nav bar.
4th Nov 2008
vb,
You would use something like:
if ( is_page('PageName') ) {
//category code }
elseif ( is_page('PageName') ) {
//category code }
4th Nov 2008
Let’s say I want excerpts instead of the full content on the home, archive and search pages, but not for my asides category, where I always want the full content. Why doesn’t this work:
if(is_front_page() || is_archive() || is_search()) && !in_category('2'))29th Dec 2008
Erin,
It is probably the
&& !part try&& !(in_category(2)))29th Dec 2008
I have been trying to get something to work, but can’t seem to get it to do so…
I have an archive page that has multiple loops. Each loop is to show only the posts that have a specific tag within the category that I am looking in. For example:
I am in the archive for the category of Cheese. The first loop should show the posts in the category that have the tag of Mouse. The second loop should show only the posts in the category that have the tag of Rat.
I want this to do this no matter which category I am in since the tags will remain the same throughout.
I tried…
if (is_category() && is_tag(‘mouse’));
and…
if (is_category() && is_tag(‘rats’));
But this isn’t working… It is showing posts for the second loop in the first loop despite having it set to not show duplicate posts.
any ideas?
14th Dec 2009
Kahil,
I think what you want is the query_posts function with the “category_name” or “cat” option in your loops.
You might also be able to use “has_tag()” in place of “is_tag()”
14th Dec 2009
I’ve tried both of those things with no luck. Maybe I’m not doing it right though. I’ve been searching for almost a week and cannot find any example close to what I am wanting to do…
14th Dec 2009
I would love to know how to write a conditional statement for comments only where if the commenter is a certain user, then the comment will display their avatar and a caption below their avatar.
25th Jun 2010