How to Add Conditional Logic to Your WordPress Theme


By

Building a website with WordPress provides great flexibility with what you can do in terms of both design and functionality. And, as you get deeper into theme development, you’ll find that there are times when you need to target a specific page, category, or post type for something special. That’s where conditional logic can make your job much easier.

Conditional logic is a powerful feature of programming languages (PHP, in our case). In essence, we can use code to check if a certain condition exists and then do something specific if that condition is met. A simple example of this would be checking to see if a user is currently on our site’s home page. Using conditional logic, we might choose to load a slider or other special content in that situation.

With regards to WordPress, there are a number of useful Conditional Tags built into the software that let us test for all sorts of scenarios. They’re well-documented and allow you to bring a higher level of functionality to your site.

Today, we’ll take a look at a few of the more commonly used Conditional Tags and how you might implement them into your own site.


A Note on Using Condtional Tags

Conditional Tags are bits of PHP code and should go into your site’s active theme. There are a number of places you can use conditional logic within a theme.

If you haven’t already done so, it’s a good idea to familiarize yourself with the WordPress Template Hierarchy for an explanation of how theme files work.

And, before you try to implement new code on a live site, it’s best to set up a development (staging) environment to test things out first. Remember that one false move with PHP can give your site the dreaded “White Screen of Death”. So take great care in testing before you work on a live site.

is_home()

This one causes great confusion amongst developers. By looking at the tag, you’d assume that is_home() would be used for checking your site’s home page. That’s not the case.

It actually looks for the page assigned as your Posts page, which is set in Settings > Reading inside the WordPress dashboard. By default, WordPress uses your home page to list posts – but most sites tend to use a static home page instead.

So, for example, if you had assigned a page named “Blog” as your Posts page, that’s what is_home() will look for.

<?php

// If this the Posts page, display a message.
if ( is_home() ): 

echo '<h2>Thanks for visiting our blog!</h2>';

endif;

?>

is_front_page()

Conversely, is_front_page() looks for whichever page is set as your site’s home – regardless of whether it is static or not. Admittedly, this isn’t the most obvious thing in the world when you’re learning to create your own themes. But knowing which tag to use is vital.

What’s nice about this particular tag is that, even if you were to assign a different page as your home page later on, you’ll still target the right place. There are more explicit ways to target pages, but this one keeps you covered in the event of a change.

<?php
// If this is the home page, show a special message.
if ( is_front_page() ) { ?>

<h1>Welcome</h1>

<?php 
// Otherwise, show the page title.
} else { ?>

<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>

<?php } ?>

is_single() and is_page()

Each of these tags performs a dual function. is_single() looks to see if the current URL is a post. It could be a standard blog post, attachment or belong to a custom post type.

Similarly, is_page() will return true if the current URL is a page. By default, both tags target any content falling under the umbrella of a page or post. This is a common thread amongst many Conditional Tags.

<?php

// Target all posts.
if ( is_single() ):

echo 'You are on a single post.';

endif;

// Target all pages.
if ( is_page() ): 

echo 'This is a page';

endif; ?>

But you can also leverage these tags to target a specific post or page by adding an ID or slug.

<?php

// Target About Us
if ( is_page( 'about-us' ) ): ?>

<a href="#"></a>

<?php endif; ?>

Using an array will allow you to target multiple pieces of content.

<?php

// Target About Us and Contact Us pages.
if ( is_page( array('about-us', 'contact-us') ) ): ?>

<a href="#"></a>

<?php endif; ?>

is_tax() and has_term()

Taxonomies play a big role in how content is organized on a site. Two of the most common are categories and tags – which have their own Conditional Tags. But WordPress also enables us to create our own taxonomies.

For instance, if your site contains music reviews, you might have a taxonomy called “Genre”. From there, you could have listed specific genres of music within that taxonomy such as, “Rock”, “Hip-Hop” and “Jazz”.

is_tax() targets a taxonomy archive page. Staying with our music review example, this tag would fire (go into effect) when we are on the archive page for each of the specific genres.

<?php

// Display a message on our Genre archive pages.
if (is_tax('genre','rock','hip-hop','jazz') ):

echo '<h2>This is a genre archive. Enjoy!</h2>';

endif; ?>

has_term() is used to target the current post based on which taxonomy it belongs to. This is useful if, for example, we want to add a special graphic for posts in each genre of music on our site.

<?php

// Show an image for the Rock genre.
if (has_term('rock','genre') ): ?>

<?php endif; ?>

is_page_template()

Once you’re familiar with the template hierarchy of a theme, you may want to create your own custom page templates. is_page_template() lets you target the current page template being used (regardless of whether or not it’s custom). You can either check to see if any page template is being used at all, or look for a specific one.

You do have to be careful when looking for a specific page template, however. Since this tag checks for the existence of a filename, the conditional could stop working should that template’s name be changed. You’ll have to update your code to reflect any filename changes.

<?php

// Add a search form to the Services page template.
if (is_page_template('services.php') ):

?>

<?php get_search_form(); ?>

<?php endif; ?>

All Conditions Met

While this was just a brief introduction of how conditional logic can be used, it hopefully whets your appetite for learning more.

Once you master conditionals, you’ll be able to customize your site in ways that would be near impossible otherwise.

They can give you fine-grain control over the look and layout of your theme while still benefitting from all the advantages of utilizing a CMS.


Top
This page may contain affiliate links. At no extra cost to you, we may earn a commission from any purchase via the links on our site. You can read our Disclosure Policy at any time.