Watkins Web 3: Blog

Cheat Sheet: Wordpress Conditional Functions

As we’ve discussed, Wordpress templates control every page of that template type. For example, if you have three categories — News, Features, and Quick Links — the template category.php is going to control the HTML output that results when visiting all three URLs: http://example.com/category/news/, http://example.com/category/features/, and http://example.com/category/quick-links/. Most likely the only difference between those pages will be the text at the top (<h1>Posts categorized as News</h1> vs. <h1>Posts categorized as Features</h1>) and of course the particular posts that get listed as belonging to the category.

This rigid logic is the default for almost every template including single.php, tag.php, category.php, and search.php. However, Wordpress provides a host of conditional functions that let you check to see whether a particular category/tag/page (etc.) is being displayed. You can therefore output additional content, hide content, alter styling by adding classes, etc. It’s up to you.

Category Conditional

Lets use our “Quick Links” category as an example. Suppose we have written our category.php to display our list of posts in the standard way: we just link each post name to its corresponding permalink page (using the_permalink()) so that visitors can read the whole post by clicking on any of those links.

But suppose our “Quick Links” posts consist only of a post title and custom field called link_url that stores the URL of the external page we are linking to; in other words, Quick Links don’t use the main content field at all, so there’s no particular need to view their permalink pages. For Quick Links, we might want the titles to be linked directly to those external websites. Here’s how we might use the is_category() function inside category.php to alter our code:

<?php  
	<h2>Posts categorized as <strong><?php single_cat_title( '', true ); ?></strong></h2>
	<ul class="post-list">
		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
			if ( is_category( 'Quick Links' ) ) {
				$link_url = get_post_meta($post->ID, 'link_url', true); ?>
				<li><a href="<?php echo $link_url; ?>"><?php the_title(); ?></a></li>
			<?php }
			else { ?>
				<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
			<?php }
			endwhile; else: ?>
		<p>Sorry, no posts matched your criteria.</p>
		<?php endif; ?>
	</ul> <-- /.post-list -->
?>

More Functions

Here are some other common conditional functions along with some examples:

is_page()

Detects whether the URL being displayed is using the page.php template or shows a particular page:

<?php  	
	if ( is_page( 'About' ) ) {
		echo ("This is the about page.");
	 }
	else {
		echo ("This is not the about page.");
	}
?>

is_single()

Detects whether the URL being displayed is using the single.php template or shows a particular post:

<?php  	
	if ( is_single( '12' ) ) {
		echo ("This is the post that has ID 12.");
	 }
	if ( is_single( 'I love Design' ) ) {
		echo ("This is the post that has the title I Love Design.");
	 }
?>

has_tag()

Detects whether the current post has a particular tag. So probably most useful on single.php.

<?php  	
	if ( has_tag( 'web' ) ) {
		echo ("This post is tagged as web.");
	 }
?>

is_tag()

Detects whether the URL being displayed is using the tag.php template, or is a particular tag page:

<?php  	
	if ( is_tag( 'web' ) ) {
		echo ("Now listing posts tagged as web.");
	 }
?>

Further Reading