WordPress Conditional Tags – Tips & Tricks

Follow @tristarweb

Wordpress Conditional Tags

The conditional tags feature has existed in WordPress for a while now. However, despite being an extremely useful and quite powerful feature, it often remains underused and ignored. I thought I’d present a few examples of what you can achieve with conditional tags and why they’re so useful, so read on…

Conditional Tags?

Conditional tags are essentially PHP if statements (more information here), that use WordPress functions to check for certain criteria. This means that you can use these tags to check what page or post type you’re currently on, or if certain data exists. After checking, particular data can then be displayed (or not displayed), such as images, extra information, unique functionality, etc. There are a wide array of possibilities. Conditional tags are normally included in WordPress theme files, to reduce the need for creating custom template files (a simple conditional statement can be used instead of creating a new custom page template, a leaner and neater option).

Example Code

It’s not much use knowing what these tags can do if you don’t know how to use them. Here are some examples codes that should outline how to use conditional tags. Feel free to rob, use and abuse these tags at will!

<?php if (is_front_page()) { ?>
This snippet of text would only appear on your site's homepage.
<?php } ?>

The example above is looking for the homepage (regardless of if it’s a static homepage, or your latest posts) of your WordPress website. The text inside the PHP tags would then be displayed on the homepage, while not appearing elsewhere on the site. You could switch “front_page” to various other tags listed in the WordPress Codex – single (display only on singular post entries), page (display on pages only), category (display on category archives only), search, 404, tax, tag, etc. The concept is so simple you could probably guess most of the conditional tags!

You can reference particular pages using ID’s or page names, like below…

<?php if ( is_page( 'about' ) ) {  ?>
If your page was titled About, this text would appear!
<?php } ?>

You can mix these conditions together, to check for multiple sets of data. For example, you may want to display content on pages, single posts, and category archives… in which case you could use this:

<?php if (is page() || is_single() || is_category() { ?>
This text would be displayed.
<?php } ?>

Or, to get a little bit more technical, we can declare that different things should happen in a collection of multiple conditions.

<?php if (is_page()) { echo
"This is a standard WordPress page. We could output an image in the template... <img src='random.jpg' alt='Random' />"
; } elseif (is_single()) { echo
"This would appear on singular posts."
; } elseif (is_category()) { echo
"While this text would be echoed on category archives."
; } ?>

Why So Useful?

While the examples above demonstrate how to implement conditional tags, you likely won’t find displaying sentences here and there particularly useful…

Load scripts & stylesheets

This is a method I regularly use on WordPress sites. You may need to load a particular set of JavaScript files, or CSS stylesheets (in your theme’s header.php) for one page on your site, but not require them elsewhere (e.g. for a jQuery contact form, slideshow, etc). I’ve used the following code to load some contact form files for my contact page only…

<?php if ( is_page( 'contact_us' ) ) {  ?>
<link rel='stylesheet' href='/contactformstyles.css' type='text/css' media='all' />
<script type='text/javascript' src=k'/jquerycontactform.js'></script>
<script type='text/javascript' src='/formspec.js'></script>
<?php } ?>

Add a PHP Include

A similar situation, with this case I wanted to add a PHP include on a particular page on my site. Every other page layout was essentially identical, so with only one page.php file, I decided to just use a conditional tag instead of creating a separate page template.

<?php if ( is_page( 'services' ) ) {  ?>
<?php require_once('servicesintro.php'); ?>
<?php } ?>

Display Data in a Custom Post Type

Custom post types and conditional tags can mix together wonderfully. Due to the often “unique” nature and content of custom post types, I often like to have a particular chunk of content displayed on a set of custom posts. To put it into context, you may have a custom post type for “Testimonials”. At the end of each testimonial post, you may want to display a small message for browsers encouraging them to contact you. So, the following conditional code could be used…

<?php if ( 'testimonial_entry' == get_post_type() ) { ?>
<div id="testimonialcontact">
<p>If you're impressed by what our clients think of our services, then do not hesitate to contact us to discuss your businesses requirements. Give us call or <a href="/contact" title="Contact Us">click here</a> to send us an email!</p>
</div>
<?php } ?>

Check for Custom Fields

Despite the fact this trick doesn’t actually use conditional tag functionality, it’s along a very similar theme, and is definitely worth sharing.

I’ll regularly attach custom field data to my posts and custom post types in WordPress. It’s a great way of displaying bespoke information without trying to insert divs and styling into your actual main post area.

For example, on one of my site’s single.php files, I had an area after the main post to display a link to the client’s website (each post explained a different client). When uploaded a new post, I simple entered the client’s URL into the relevant custom field, and the link would be outputted used the following code in single.php…

<div class="projectdetails">
<p>Client: <a href="<?php get_post_meta($post->ID, 'website-url', true); ?>" title="Visit Website">Visit Website</a></p>
</div>

There is one main problem with this method. If data isn’t entered into the URL custom field, then the “projectdetails” div will still be displayed, along with the hyperlink, but the link will be broken as no URL has been entered. A simple fix exists. We need to add some PHP that checks if the custom field data exists (in which case the information will be displayed), but if there is no data, then a small notice is displayed instead.

This is my final code…

<?php if( get_post_meta($post->ID, 'website_url', true) ) { ?>
<div class="projectdetails">
<p>Client: <a href="<?php get_post_meta($post->ID, 'website-url', true); ?>" title="Visit Website">Visit Website</a></p>
</div>
<?php } else { ?>
<p>Project not currently complete.</p>
<?php } ?>

Pretty simple! This code can be modified heavily to create some much more complex (but very awesome) scenarios.

Hopefully this blog has opened your eyes to the usefulness of using WordPress conditional tags, and ways in which they can be used. If you have any questions regarding the topic, feel free to leave a comment below!

About Jack

Howdy... I'm Jack. I have interests in various areas of web design, but mainly you'll find me playing with Wordpress, Magento, and the latest developments in languages such as HTML5 and CSS3. I'm also a keen movie watcher, and regularly watch Newcastle FC lose at the weekend. Check out my blog posts and get ready to be blown away...

One Response to WordPress Conditional Tags – Tips & Tricks

  1. JD says:

    23:45 13/06/2011

    Hi Jack,

    I want to make a piece of php code function in a post template depending on whether or not a reader is looking at a post from a certain category. Can you do this with conditional tags? So if this is my code:

    “”

    How would I make this work only for a post from my category ‘tech’?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please wrap all source code with [code][/code] tags.

Follow Tristar on Twitter