Using conditional tags with Wordpress
This is something I had been trying to wok out how to do for a while. It’s particularly useful for implementing links only on certain pages or for keeping stuff on your home page but off of the rest of your blog, or vice versa.
Conditional tags are a way of having bits of your blog show up only in certain places. The task I had yesterday was to implement some RSS code on only certain pages of a website. I needed to keep the code kept off of the front page and only under a certain category, let’s say “category A”, for example.
It was so easy to implement that I’m actually a little embarrassed that I didn’t learn this basic code earlier. Bear in mind that this code will only work when implemented in your template files and not in actual blog posts.
There are a range of conditional tags that you can use, all of which can be found on the Wordpress website. Here’s the one I needed:
<?php if(is_category(’category A’) ) { ?>
Code went here
<?php } ?>
Let’s say you want something to appear only on your home page. The code would then look like this with conditional tags:
<?php if(is_home() ) { ?>
Code goes here
<?php } ?>
It’s that simple. Conditional tags can come in handy for cleaning up your blog’s template. The possibilities are endless. If you want to use multiple conditional tags, you can do so as follows:
<?php if(is_category(’category A’) || is_single()) { ?>
Code here again
<?php } ?>
This would have the code show up only in category A and on single blog posts. Easy!
Share:
Digg it for goodness sake! Stumble your heart out!





Discussion Area - Leave a Comment