Refresh Events

Refresh Events encourages collaborative partnerships, fosters education at all skill levels and creates networking opportunities within the Toronto interactive community.

Stay Fresh - WordPress Conditional Tags

If you are like us, you probably want to exert a fair amount of control over your website; after all, it is an important part of your personal (or corporate) brand and deserves an equally fair amount of care and attention.

With that in mind, we searched for a solution to our problem: We wanted better control over our title and meta tags for SEO purposes. We were hesitant to use a plugin, because we felt that could be a more efficient way of handling such a small but important change. We quickly discovered that WordPress has a series of functions called Conditional Tags.

If you look at the source code of a few of the pages on this site, you will see that the title and meta tags differ slightly from page to page and section to set. This is done through the use of those Conditional Tags. To give you a brief overview before I jump into the code, Conditional Tags give you the ability to display certain types of content (images, etc) depending on the conditions that the page meets. Essentially, Conditional Tags are a series of if statements targeting the various sections of your site. I won’t go into explaining all of them, the WordPress Codex does an excellent job of that, and I recommend you take a look at the plethora of documentation available there.

The Code

<?php if (is_page('home')) { ?>
	<title>Refresh Events: Inspire | Educate | Connect</title>
	<meta name="description" content="Refresh Events is a Toronto-based social justice organization dedicated to the promotion and advancement of the interactive industry and its members through affordable, accessible and relevant digital media programming." />
<?php } elseif (is_page('about')) { ?>
	<title>About: Refresh Events</title>
	<meta name="description" content="More information about Refresh Events, the team and how to contact us." />
<?php } elseif (is_page('events')) { ?>
	<title>Future Events: Refresh Events</title>
	<meta name="description" content="Listing of all future events being presented by Refresh Events." />
<?php } elseif (is_page('past-events')) { ?>
	<title>Past Events: Refresh Events</title>
	<meta name="description" content="Archive of all previously passed events presented by Refresh Events." />
<?php } elseif (is_page('privacy-policy')) { ?>
	<title>Privacy Policy: Refresh Events</title>
	<meta name="description" content="Privacy policy and what we do with the information you supply us with." />
<?php } elseif (is_page('terms-of-use')) { ?>
	<title>Terms of Use: Refresh Events</title>
	<meta name="description" content="Terms of Use for our website." />
<?php } elseif (is_page('archives')) { ?>
	<title>Blog Archive: Blog: Refresh Events</title>
	<meta name="description" content="Complete archives of the Refresh Events blog." />
<?php } elseif (is_year()) { ?>
	<title>Yearly Archive for <?php the_time('Y'); ?>: Blog: Refresh Events</title>
	<meta name="description" content="Yearly Archive for <?php the_time('Y'); ?> on the Refresh Events blog." />
<?php } elseif (is_month()) { ?>
	<title>Monthly Archive for <?php the_time('F Y'); ?> : Blog: Refresh Events</title>
	<meta name="description" content="Monthly Archive for &amp;#39; <?php the_time('F Y'); ?> &amp;#39; on the Refresh Events blog" />
<?php } elseif (is_day()) { ?>
	<title>Daily Archive for <?php the_time('F jS, Y'); ?>: Blog: Refresh Events</title>
	<meta name="description" content="Daily Archive for &amp;#39;<?php the_time('F jS, Y'); ?>&amp;#39; on the Refresh Events blog." />
<?php } elseif (is_category()) { ?>
	<title>Category Archive for &amp;amp;#8216;<?php single_cat_title(); ?>&amp;amp;#8217;: Blog: Refresh Events</title>
	<meta name="description" content="Category archives for &amp;#39;<?php single_cat_title(); ?>&amp;#39; on the Refresh Events blog." />
<?php } elseif (is_tag()) { ?>
	<title>Tag Archive for &amp;amp;#8216;<?php single_tag_title(); ?>&amp;amp;#8217;: Blog: Refresh Events, Interactive Architect</title>
	<meta name="description" content="Tag archives for &amp;amp;#39;<?php single_tag_title(); ?>&amp;amp;#39; on the Refresh Events blog." />
<?php } elseif (is_single()) { ?>
	<title><?php the_title(); ?>: Blog: Refresh Events</title>
	<meta name="description" content="<?php the_excerpt(); ?>" />
<?php } else { ?>
	<title>Blog: Refresh Events</title>
	<meta name="description" content="Refresh Events blog." />
<?php } ?>

What’s all this, then?

If you’re familiar with PHP (or even if you aren’t), we’ve used an if statement to target the various pages on our site. For instance, we’ve used the is_home() function to display the appropriate title and page description to search engines when this page is indexed.

You can also do this with the is_day(), is_time(), is_month() and is_year() (also known as “Date Pages” pages, as we have done here:

<!-- yearly archives page -->
<?php } elseif (is_year()) { ?>
	<title>Yearly Archive for <?php the_time('Y'); ?>: Blog: Refresh Events</title>
	<meta name="description" content="Yearly Archive for <?php the_time('Y'); ?> on the Refresh Events blog." />
<!-- monthly archives page -->
<?php } elseif (is_month()) { ?>
	<title>Monthly Archive for <?php the_time('F Y'); ?> : Blog: Refresh Events</title>
	<meta name="description" content="Monthly Archive for &amp;#39; <?php the_time('F Y'); ?> &amp;#39; on the Refresh Events blog" />
<!-- daily archives page -->
<?php } elseif (is_day()) { ?>
	<title>Daily Archive for <?php the_time('F jS, Y'); ?>: Blog: Refresh Events</title>
	<meta name="description" content="Daily Archive for &amp;#39;<?php the_time('F jS, Y'); ?>&amp;#39; on the Refresh Events blog." />

Of course, you are not limited to using Conditional Tags for meta data. They can be used for displaying just about any type of content. If you wanted to display the description of a category in your category archive page, you could use:

<?php /* If this is a category archive */ if (is_category()) { ?>
	<h2 class="pagetitle">Archive for the &amp;amp;amp;amp;#8216;<?php single_cat_title(); ?>&amp;amp;amp;amp;#8217; Category</h2>
	<?php echo category_description($category); ?>

Of course, you’d have to remember to fill out the description field of each category. This can be found in your WordPress Admin Panel at Dashboard → Posts → Categories.

So as you can see, Conditional Tags when used with Template Tags, create a very powerful combination that allows you to display almost anything that you would like.

I’d be interested in hearing your thoughts, comments, and questions about this post. Please post them up below in the comments section. ↓

Comments are closed.