Using Conditional Tags In WordPress

Lets take a look at how to execute custom functions in your themes different hook locations based on certain conditions being met and returning yes.

If you use a premium theme framework like Genesis, you’ll have access to a users guide.

The users guide generally contains code snippets containing custom functions.

Note: If you’re not using a premium theme framework then you’ll need to create the custom functions yourself or find a code snippet which works with your theme.

These custom functions can be executed in specific theme locations using your themes action hooks.

All you need to do is paste the code snippet into your child themes functions.php file to customize your theme functions.

Each theme framework uses different hooks however they pretty much do the same thing, they’re just given different names like:

thesis_after_post
genesis_after_post
woo_post_after

These 3 examples all execute after the post has finished where you may want to add a widget area, banner or email opt in box with social sharing/following buttons.

But what if you only want the custom function to execute in a specific hook location based on certain conditions like? For example:

  • Only on single posts: is_single()
  • Only on posts in a specific category: is_category()
  • Only on an archive page: is_archive()
  • Only on the home page: is_home()

Adding a conditional tag to your custom function which executes in your themes hook location, enables you to execute the function only if the condition returns yes.

Note: You may want to test these tags with the code snippets for your custom functions on a local setup before you add the code to a live site.

Once you’re comfortable using custom functions and understand where and how your themes action hooks work, you can go one step further by using conditional tags or conditional statements as some developers call them.

So how do i use conditional statements?

Conditional Tag Examples

Here’s some common conditional tags used by WordPress designers as well as theme and plugin developers.

is_home()
is_category()
is_single()
is_page()

Firstly, lets take a look at using conditional tags based on multiple statements.

is_single & in_category

if (is_single() && in_category('134'))
if (is_single() && in_category('web-design'))

Both these code snippets do exactly the same thing and contain 2 conditional tags.

The first uses the category i.d of 134 and the second uses the category slug ‘web-design’.

Any function you include with this code will only execute on single posts in the category which contains a post i.d of 134 or page slug ‘web-design’. This is clearly the web design category.

Parameters

As you can see in the above example, you can use different parameters with conditional tags like:

  1. Post ID
  2. Post Title
  3. Post Slug

Some conditional tags like the is_home() tag don’t except any parameters probably because they don’t really need any.

is_page

if (is_page('4289'))
if (is_page('contact'))

Both these conditionals tags do exactly the same thing as well.

The first uses the pages post i.d of 4289 and the second the page name and slug ‘contact’.

The functions you include with this tag will only execute on the contact page.

Home Page Conditional Tags

The most common uses for using the is_home tags would be for displaying something only on the home page or excluding the home page from the function.

is_home

if (!is_home()

This code contains the ! before the home_page conditional tag so its excludes the home page from the function.

if (is_home()

This code creates a condition that only applies to the home page only.

Working Example

If you copy and paste the following code using a code editor, into your child themes functions file, you’ll find it prints text after the header only on single posts.

add_action( 'genesis_entry_footer', 'add_content_after_single_posts' );
function add_content_after_single_posts() {
if ( is_singular('post') ) {
echo 'Content Added After Single Post Entry Footer';
	}
}

The above code uses the genesis_entry_footer hook position with the is_singular('post') conditional tag and prints the text after the content on single posts only. This is a classic example showing how to use conditional tags ina custom function with your themes action hooks.

Conditional Tags With Arrays

You can also add an array to your conditional statement.

is_page (array

if (is_page(array('contact','37290'))

You could add more pages to this array simply by including them within this array separated by a comma.

This function will only execute on the contact page and the page which contains a post i.d of 37290.

Outputting Text/HTML

If you simply want to display some text or HTML, you can add a conditional statement to your themes actions hooks. The easiest way to do this would be to paste the conditional tag with your HTML using a hook plugin.

if ( ! is_home()) { 
echo'Your Text Here';
}

Let’s take a common function with action hook and add a conditional tag.

Conditional Tags in Genesis

You can easily add one of the Genesis theme code snippets for use in StudioPress themes and include a conditional tag with the code.

/** Add custom body class to video category */
add_filter( 'body_class', 'add_body_class' );
function add_body_class( $classes ) {
if ( is_category( 'video-tutorials' ) )
$classes[] = 'custom-class';
return $classes;
}

This code enables the user to style the video category page using a custom class.

The parameter used is the video-tutorial slug however you could use the post.i.d or title in your conditional tag.

Conditional Tags in Thesis

Lets take a custom function from the DIY themes users guide and add a conditional statement.

function hide_nav_menu() {
if(is_page ( 'contact-page' ) )
remove_action ( 'thesis_hook_before_header', 'thesis_nav_menu' );
}
add_action ( 'thesis_hook_before_html','hide_nav_menu' );

This will hide the nav menu on the contact page only.

We could use different parameters for the contact page including the page slug, post i.d or page name.

Where To Put The Code?

If you’re using a premium theme framework, you can paste the code into your child themes functions.php file.

These frameworks all offer a hook plugin or manager which enables you to paste the code into the hook location where you want the code executed based on your conditional tags.

Clearly, you won’t need to include the action hook in your code if you use a hook manager.

Another option is to paste the conditional tags directly into your themes template files in the location you want to execute the function based on your conditions. This is not best practice as you will lose the code when you update the theme.

Always best practice to create a child theme and make modifications to your template files there. Better still, use the custom functions your theme developer has already created and paste the code into your child theme functions file with your conditional tags, once testing proves it works.

WordPress Conditional Tags & Examples

You’ll find a list of all WordPress conditional tags with examples in the WordPress Codex.

Theme Specific Conditional Tags

Some premium themes also use thier own conditional tags which are unique to their theme framework.

Woo Commerce is one of these: Examples:

is_woocommerce() - Returns true if on a page which uses WooCommerce templates

is_product_category() - Returns true when viewing a product category archive

is_checkout() - Returns true on the checkout page.

Learn more about using conditional tags with Woo Commerce.

Conclusion

Using a theme which offers a large range of code snippets tested by the developer is a huge bonus. This way you can choose any of the conditional tags WordPress offers and add it to your code snippet to customize your theme any way you want to.


Comments

2 responses to “Using Conditional Tags In WordPress”

  1. Austin Gunter (@austingunter) Avatar
    Austin Gunter (@austingunter)

    What a great in-depth post, Brad. WP Sites is making amazing progress, both design and content-wise. Keep it up!

    1. Thanks Austin.

      One of the main reasons everything is progressing so much faster is because i don’t have to wait around for 3-5 seconds anymore for my pages to load!

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.