Posted on August 16, 2008 - by Impress Lab
Conditional Formatting in WordPress
Conditional formatting is a powerful, and perhaps rarely used, template tag that can make WordPress more like a dynamic Content Management System. With conditional formatting you can display certain item, contained within the tag, depending on what type of current page is displayed (such as if it is a static page, blog post, category, or the even the homepage).
For this tutorial consider that we want to display only certain information in the sidebar of the theme; we will be editing only the sidebar.php WordPress template file.
Tutorial 1: Display Child Pages only on Pages
A very practical use of conditional formatting is to display child pages in the sidebar. For example, if we want the sidebar to have a section for “More Information,” with links to all child pages, we can use the standard WordPress code to display child pages:
<ul>
post_parent)
$children = wp_list_pages("title_li=&depth=1&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_pages("depth=1&title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?></ul>
/* End code for child pages */
This code will display the More Information header with child pages as a list underneath. Even if there are no child pages below the current page, the More Information header will still be displayed. While you can use the “title_li=” part of the wp_list_pages tag to display the header, you may not be able to style it as if it where a separate heading item.
Either way, let’s suppose we want to display the above information only on pages, not categories or blog posts. This is where conditional formatting comes in. By using an “if” statement, we can display the child pages code above only when we are on a page template (page.php). WordPress will automatically check that it is currently showing a page to the user and display the code in the “if” statement in the sidebar; if the user is not on a page, the code will not display. Conditional formatting requires a very simple “if” statement:
<h3>More Information</h3> /* Start child pages code */
<ul>
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&depth=1&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_pages("depth=1&title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<?php echo $children; ?>
<?php } ?>
</ul>/* End child pages code */
<?php } ?> /* End if statement */
When displayed, “More Information” and the child pages link list will only be displayed when the user is on a page (and using the page.php WordPress template).
Tutorial 2: Displaying Recent Posts only on Blog Posts
Similar to the previous tutorial, you can use conditional formatting to display certain code on the blog posts (i.e. all blog posts that use the single.php WordPress template).
The standard PHP code to display, say, the 5 most recent blog posts ordered by date (default) is:
<ul>
<?php wp_get_archives(‘type=postbypost&limit=5′); ?> /* Display 5 most recent posts */
</ul> /* End recent posts code */
Now, lets only display the above code when the user is on a blog post (using the single.php template):
<h3>Recent Blog Posts</h3> /* Start recent posts code */
<ul>
<?php wp_get_archives(‘type=postbypost&limit=5′); ?> /* Display 5 most recent posts */
</ul> /* End recent posts code */
<?php } ?> /* End if statement */
The 5 most recent blog posts will now only be displayed in the sidebar as a list when the user is on a current blog post (single.php).
Other Location Tags
In addition to using conditional formatting to display information only on pages (using if ( is_page() ) ), on blog posts (using if ( is_single() ) ), you can also use the following location tags:
- is_category()
- is_search()
- is_home()
- is_page()
- is_single()
Multiple Location Tags
If you want to display certain information on more than one location tag, you can do so with a minor adjustment. For this example, lets assume we want to display the 5 most recent blog posts on both the sidebars for blog posts template (single.php) and on the search results (search.php). In the sidebar, we would use this code:
<h3>Recent Blog Posts</h3> /* Start recent posts code */
<ul>
<?php wp_get_archives(‘type=postbypost&limit=5′); ?> /* Display 5 most recent posts */
</ul> /* End recent posts code */
<?php } ?> /* End if statement */
Notice that we use one “if” statement: the above code tells WordPress that if the users is using single.php or search.php, display the enclosed code.
Sample Sidebar
To further showcase the dynamics of using conditional formatting, and its simplicity, below is a hypothetical sidebar that uses conditional formatting heavily:
<?php include (TEMPLATEPATH . ‘/searchform.php’); ?>
</div> /* End search form */
<?php if ( is_home() ) { ?> /* Start if statement: display on homepage only */
<h3>About Us</h3>
<p>Text.</p>
<?php } ?> /* End if statement */
<?php if ( is_page() ) { ?> /* Start if statement: display on pages only */
<h3>More Information</h3> /* Start child pages code */
<ul>
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&depth=1&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_pages("depth=1&title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<?php echo $children; ?>
<?php } ?>
</ul>/* End child pages code */
<?php } ?> /* End if statement */
<?php if ( is_home() || is_page() || is_single() || is_category() || is_search() ) { ?> /* Start if statement: display on homepage, pages, blog posts, index, results */
<h3>Recent Blog Posts</h3> /* Start recent posts code */
<ul>
<?php wp_get_archives(‘type=postbypost&limit=5′); ?> /* Display 5 most recent posts */
</ul> /* End recent posts code */
<?php } ?> /* End if statement */

Visit My Website
August 19, 2008
behold the power of WordPress! Bow down!
I use conditional formatting extensively and it make more dynamic templates. must use!!
Visit My Website
August 25, 2008
I have used conditional formatting before and someone once commented that he thought I am using multiple sidebar templates (but it is all in one, which makes it easier to update in the future).
Visit My Website
August 27, 2008
thanks for the easy tutorial.
Visit My Website
October 11, 2008
Thank you for this outline. I was having a hard time implementing the child conditional. This helped a ton!