Use Conditional Tags to Empower Your Content

Oct 8th, 07 | 2 remarks

Have you ever wanted to display different content for certain pages on your blog? Why not right? Contextually relevant links, ads, or content is always a good idea. Until recently, I had been creating separate sidebar templates to accomplish this, but it’s the hard way ’round.

When I began coding Pink Adaptations, I discovered conditional tags and the power and flexibility they afford. If you’re not familiar, conditional tags are essentially a way to show content based on whether a specific requirement is met.

It’s done by using an if statement (not unlike The Loop). You may have seen this bit of code if you’ve ever looked through the default theme packaged with Wordpress:

<?php /* If this is a category archive */ } { ?>
	<p >You are currently browsing the archives for the
	<? php single_cat_title(''); ? > category.</p >
? php } ? >

I stupidly ignored this for a long time because I didn’t fully understand its purpose, but this one snippet can be tweaked to display predetermined content not just for the archive page, but also your index, single post, search, Pages, and even custom templates.

For example, on my Themes page, I wanted to show a sidebar blurb about custom theming, a donation option, etc. I didn’t want this info on every page across my site, so I added the following conditional tag in sidebar.php:

< ?php if (is_page('wordpress-themes')) { ? >
	< p>Blah blah blah.</ p>
< ?php } ? >

This refers to the slug of my Themes page, and when the condition is met it displays only the content I specified.

Conditional tags are a very efficient method of controlling various sidebar content for different pages - and how nice to be able to manage all of it from one place! Not to mention the fact that your pages will load faster, since only one file is being called site-wide. :)

p.s. If you copy/paste the snippets above don’t forget to remove the spaces.


  1. Randa Clay

    Look at you! Gettin’ all fancy with the php code! :) Thanks for this tip - I’ll bookmark it as I’m sure I’ll need it sometime soon.


  2. Charity

    LOL I must be on a roll! :)

    Once I figured out the best way to set up the if statements, this method saved me a lot of time in building out the theme(and making changes afterward). I think you’ll love it Randa, and if you need a hand with it just give me a shout!

Post your remark

Other Recent Articles

In Reviews »

Clever Development With Coda

07/15/08
If you follow design related blogs, it seems you can't click a mouse anymore without Coda's icon making an appearance. I heard about this software long before buying my Mac, but because it's exclusive to that platform, I was never able to give it a test run. Now eight ... [ » ]

In Inspiration »

Have You Ever?

06/25/08
If you know me very well, you know how important music is in my life. It's playing at all times in my house, and I embrace all kinds of genres. I don't talk about it much on this blog, but I should. Music and creativity, in my opinion, are inextricably ... [ » ]

In Design »

The Evolution of a Logo (I Can Live With)

06/19/08
One of the most frustrating things for me since I started writing on this blog was the lack of a logo I really loved. That's not to say I haven't tried. But logo design, to me, is kind of a thing all it's own and I'm just not very good ... [ » ]