Wordpress Conditional Tags: Write Less Code
While re-aligning my website I realized I was writing a lot of redundant code. Some page templates were nearly exactly the same like the category and tags pages, the only difference was one word! Copying and pasting that one template seemed like a big waste of time, and it was.
I read somewhere that good programmers are lazy programmers. Not in the sense that they don’t fix their own code when they find a better way to do it…but that they don’t repeat code, over and over and over again. Not only does it take longer to repeat code, but it is a royal pain when you need to update(like when a CSS framework gets updated and drops a class name…).
Learn it right the first time
When learning to build themes the idea of template hierarchy got pounded into my head. I had template names floating around my head and had the idea that I needed to use them. All of them. Category, Tag, Author, Page, Archive, Home, Date, Search and all the rest of them. You don’t need to have a template for each, unless there are going to be major differences between them! That was a major point I missed when learning Wordpress.
Conditional tags to the rescue
You’ve probably heard about conditional tags, used them, or seen them in other themes. I used them all along for things like dynamic titles, but somehow it escaped me to use them to make my life simpler. A conditional tag looks like this:
<?php
if (is_home()) { echo bloginfo('name'); }
That means if you are on the home page print the blogs name. You can use conditional tags for everything from adding content just for the owner of the site(edit post links) to finding out what category a post is in. Go here for a full list of tags.
A bit about the hierarchy
Pretty powerful little tool no doubt, but so far it looks like adding more code not less. Combine conditional tags with the template hierarchy and you will see what I am talking about. Wordpress calls certain files by default. For instance, if you visit a single post Wordpress will try to call single.php, if Wordpress can’t find single.php it will call index.php. You can use this to your advantage!
Say you wanted to develop a theme for Wordpress(good for you!). You design your theme and decide that your category, tag, monthly, and authors page are all going to look exactly the same except for the header. Instead of making a category.php, tag.php, date.php and author.php you are going to create archive.php. Why? Glad you asked, If Wordpress doesn’t find any of those files it will automatically call archive.php. We’ll use conditional tags to call specific content based on what type of page it is. Lets look at some code!
<div class="content">
<h1>You are viewing the Archives
<?php if(have_posts()): while(have_posts()) : the_post(); ?>
<h2>
<p class=”meta”> by
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
</div>
Theres a really basic archives template in all its glorious glory. But what if you are viewing an authors page? You don’t want to see “Viewing the Archives”, thats confusing to people(and you never want to confuse people, unless its a puzzle site). Lets stick a conditional tag in there to show the authors name at the top.
<div class="content">
<?php if (is_author()) { ?>
<h1>Posts Written By <?php the_author(); ?>
<?php } else (is_archive()) { ?>
<h1>You are viewing the Archives
<?php } ?>
<?php if(have_posts()): while(have_posts()) : the_post(); ?>
<h2>
<p class=”meta”> by
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
</div>
Now when viewing an author page you will see “All Posts By The Dude” instead of “You are viewing the Archives”. This is just a basic if else php block which can be used to do all sorts of things(I suggest playing around with ideas). But what about all the other pages? We can use an if elseif else block:
<div class="content">
<?php if (is_author()) { ?>
<h1>Posts Written By <?php the_author(); ?>
<?php } elseif (is_category()) { ?>
<h1>You are viewing the <?php single_cat_title(); ?> category
<?php } else (is_archive()) { ?>
<h1>You are viewing the Archives
<?php } ?>
<?php if(have_posts()): while(have_posts()) : the_post(); ?>
<h2>
<p class=”meta”> by
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
</div>
You can insert as many elseif statements as you need. For this example I would insert one for is_tag() and another for is_date(). Now you have five different headings and they clearly tell the user what they are looking at, and you only had to create one page!
You got homework
Sure now you don’t have to write as much code but don’t stop there. You can use this technique to dynamically show content depending on the type of page the user is looking at whether its a post, an about page, or the home page. Make custom layouts for pages or certain categories. Combine this technique with some basic php and make it even more powerful.
Note: These code examples wouldn’t actually work as the_author() has to be inside the loop. It can work, but thats another whole tutorial.Now go outside.






Leave a comment