X

The Ultimate Guide to WordPress Conditional Tags

WordPress conditional tags are a great feature of WordPress that allows you to control what content is displayed on a page. There are conditional tags for different areas of your website such as your home page, blog posts and pages. This allows you to change what is displayed around your website. For example, you could change your website logo in different areas of your website.

In this tutorial, I will explain what conditional tags are available to you and show you how they can be used in themes and plugins.

How WordPress Conditional Tags Work

Conditional tags are a boolean data type that can only return true or false. The tag is_home(), for example, refers to the blog index. We can use this tag to display a message to visitors of our blog. This message will not be displayed elsewhere.

The code is straightforward. All we are doing below is checking if the page being shown is the blog index page. If it is, we display our message.

<?php         
 
if ( is_home()) {

echo "Welcome to Our Blog!!";

}

?>

The above is a basic example of what can be achieved using conditional tags, however that is essentially all there is to it. You are simply checking the type of page that is being displayed. Depending on whether the result is true or false, another piece of code is actioned.

Before we look at more examples of how conditional tags can be used, let us first look at popular conditional tags that you are likely to see used in your WordPress themes.

  • is_home() – Checks if the blog post index is being displayed. This may or may not be your home page as well.
  • is_front_page() – Checks if your home page is being displayed. This works whether your front page settings are set up to display blog posts (i.e. blog index) or a static page.
  • is_single() – Checks to see whether any type of single post is being displayed (excluding attachments).
  • is_attachment() – Checks if an attachment is displayed.
  • is_page() – Checks if a page is being displayed.
  • is_singular() – Checks whether a single post, attachment or page is being displayed. True is returned if either of those conditions are met.
  • is_category() – Checks whether a category archive page is being displayed.
  • is_search() – Checks if a search results page is being shown.
  • is_tag() – Checks whether a tag archive is being displayed.
  • is_author() – Checks if an author archive page is being displayed.
  • is_archive() – Checks if any type of archive page is being displayed including category, tag, date and author archives.
  • is_sticky() – Checks if a post has been defined as sticky.
  • is_multi_author() – Checks if more than one author has published posts on the website. True is returned if two or more people have published posts. If only one author has published posts, or if no posts have been published at all, false is returned.

There are six time based conditional tags that you will also find useful. These tags refer to date archive pages. For example, the URL http://www.yourwebsite.com/2013/12/ is a month based archive page.

If any of the following conditional tags returns true, is_archive() would also be true.

  • is_date() – Checks if it is a date based archive page.
  • is_year() – Checks if it is a year based archive page.
  • is_month() – Checks if it is a month based archive page.
  • is_day() – Checks if it is a day based archive page.
  • is_time() – Checks if it is a time based archive page.
  • is_new_day() – Checks if today is a new day. If the current post was published on a different day from the previous post that was published, it would return true. False will be returned if both posts were published on the same day.

You will come across conditional tags such as is_home() and is_single() frequently, however you do not need to remember all of these conditional tags. Most WordPress users refer to the WordPress codex for the appropriate conditional tag when they need to set up a conditional function.

Conditional Tags Examples

Many conditional tags allow parameters to be passed to the function. This gives you much more control over what conditions have to be met before something is actioned. is_page() is a good example of this. The tag allows you to check whether the page that is being displayed is a page. is_page() will return a value of true if any page is displayed, however you need to specify the $page parameter if you want to be more specific. The $page parameter can be the page ID, page title or page slug.

Let us consider a regular website that has an about page and you want to customise the about page differently from all other pages. For example, you could display a photograph of your company at the top of the sidebar, or you could display additional information at the bottom of the about page.

To do this, you need to define the $page parameter. If the page ID was 10, you could open up your conditional statement with something like this:

if ( is_page(10) ) {

A specific page can also be specified by passing the page title to the function.

if ( is_page( 'About Us' ) ) {

The page slug can also be used. As you may recall, the page slug is the unique named identifier at the end of the URL. If your about page URL was www.yourwebsite.com/about-our-company/, the page slug would be about-our-company.

if ( is_page( 'about-our-company' ) ) {

Some conditional tags, such as is_page(), can also pass parameters in an array. The following conditional statement will return a value of true if either of the conditions are true.

if ( is_page( array( 10, 'About Us', 'about-our-company' ) ) ) {

It is common for developers to set more than one condition when using conditional tags. Let us go back to the simple task of displaying a welcome message to blog visitors. This is something that a corporate website might want to add to their blog area but not to other areas of their website (e.g. home page, contact page, about page etc).

They can do this by using the is_home() and is_single() conditional tags; which represent the blog index and single posts respectively. To display a message in both areas, you need to use the logical OR operator ||. This is illustrated in the code below. The initial if statement checks whether the page is the blog index or a single post. If either is true, the message is displayed.

<?php         
 
if ( is_home() || is_single() ) {

echo "Welcome to Our Blog!!";

}

?>

Another logical operator that is very useful is the AND operator &&. This is used when you want two or more conditions to be true before something is actioned. The following if statement checks if a page is both an archive page and categorised under the news category. In the news category pages, the welcome message will be displayed. Nothing will be displayed in other categories.

<?php 

if ( is_archive() && is_category( 'News' ) ) {
  
echo "Welcome to the News Archives";

}

?>

AND and OR operators can be combined. The example below is taken from the functions.php template of the default WordPress theme Twenty Thirteen. The function is used to display the page title in the browser, however only part of the function is shown below.

The if statement returns a value of true if there is a site description and the user is viewing the blog index or the home page. The site description can be entered via the tagline field in your general settings area. If you complete this field, on your blog index and home page the title bar will display “Site Title | Site Description” (note: the separator is displayed using the string $sep in the code below). If you do not, the title bar will display “Site Title”.

As you can see, is_home and _is_front_page are viewed as a single entity due to the OR operator. This is why they are wrapped inside brackets.

if ( $site_description && ( is_home() || is_front_page() ) )
		$title = "$title $sep $site_description";

Another PHP logical operator you can use is the not operator !. This is more practical to use in many circumstances. For example, let us say you want to display a photograph on all of your pages except your archives. There is no need to set up a long conditional statement that asks “Is this the home page, is this a single post, is this a page…”. It is more practical to simply ask “Is this not an archive page?”.

To do this, simply add an exclamation mark before the conditional tag. The code below shows how straightforward this is in practice. It will display an image on every page of your website except archive pages.

<?php 

if ( !is_archive() ) { ?>
  
<img src="photo.jpg" />

<?php 

}

?>

Up until now, we have looked at basic examples of conditional tags being used where something is either actioned or it is not actioned (i.e. if A is true, do B). In practice, there is usually another action to do if a condition is not met. Additionally, there may be several conditions that can be met, with a different response to each one.

Rather than write lots of individual statements for this, it is practical to use else and elseif statements. This allows you a greater degree of control over what is displayed on your website.

We can show this using an example. Let us say that you want to show a different logo on your website in different areas of your website. How would you do this? The answer is simple: We use else and elseif statements. The code below shows how this can be achieved.

<?php 

if ( is_home() || is_front_page() ) { ?>
  
<img src="logo-home.png" />

<?php 

} elseif ( is_category() ) { ?>

<img src="logo-category.png" />

<?php 

} elseif ( is_single() ) { ?>

<img src="logo-blog-post.png" />

<?php 

} elseif ( is_page() ) { ?>

<img src="logo-page.png" />

<?php 

}

else { ?>

<img src="logo-general.png" />

<?php 

}

?>

Depending on what area of the website a visitor is viewing, one of five logos would be displayed using the above code. It is a basic example that illustrates how easily else and elseif statements can be used to control many different areas of your website.

Elseif statements are also used in other parts of WordPress. Most functions.php templates use them and many WordPress themes use them to change how their website title is displayed in browsers.

More Conditional Tags

There are a number of additional conditional tags available. Many of these are used by developers in themes and plugins.

Below is a list of some of the other conditional tags that are available to you.

  • is_tax() – Checks whether a custom taxonomy archive page is displayed.
  • has_term() – Checks if the current post has one of the specified terms.
  • taxonomy_exists() – Checks if the taxonomy name exists.
  • post_type_exists() – Checks if a post type exists.
  • is_post_type_hierarchical( $post_type ) – Checks if the post type is hierarchical.
  • is_post_type_archive() – Checks if the archive page of a specific post type is being displayed.
  • is_comments_popup() – Checks to see if the comments popup window is open.
  • comments_open() – Checks whether comments are permitted for the current post or page.
  • pings_open() – Checks if pings are permitted for the current post or page.
  • is_feed() – Checks whether the current query is for a feed.
  • is_404() – Checks whether a 404 error is being displayed.
  • is_paged() – Checks whether the page you are currently viewing is a paginated page other than page one. Posts and pages are paginated when you use the nextpage quicktag in your content to split up large posts.
  • is_trackback() – Checks whether a trackback is being used.
  • is_admin() – Checks whether the user is logged into the administrator area. It is not used to check whether a user has administrator privileges, only whether they are logged into the WordPress dashboard.
  • is_page_template() – Checks whether the page being viewed is using a page template. A specific page template can be defined, if necessary.
  • is_preview() – Checks whether a blog post is being viewed in draft mode.
  • has_excerpt() – Checks whether the current post has an excerpt. Specific posts can be defined.
  • has_nav_menu() – Checks whether a menu location has a menu assigned. This is used by theme developers to show something in the event that the user has not added a menu.
  • in_the_loop() – Checks whether the caller is still within the WordPress loop.
  • is_active_sidebar( $index ) – Checks if a given sidebar is being used.
  • is_multisite() – Checks if multisite is supported.
  • is_main_site() – Checks if a multisite is the main site in the network.
  • is_super_admin() – Checks if a user is a super admin within the network.
  • is_plugin_active( $plugin ) – Checks whether a plugin is activated.
  • is_child_theme() – Checks if a child theme is being used.
  • current_theme_supports( $feature ) – Checks if a theme supports a specific feature such as post formats or featured images.

Also check: How to Remove Default Taxonomies.

Conditional tags are an important WordPress concept. Due to how useful they are, there are few WordPress themes that are designed without them. Once you understand else statements, elseif statements and logical operators such as AND, OR and Not; you will be able to tackle any conditional function.

I hope this guide helped you learn how to use WordPress conditional tags in your themes.

If you liked this article, then join IsItWP on Twitter.

Comments   Leave a Reply

  1. hi, im hetting a problem in executing the conditional statement…

    if ( is_home() || is_front_page() || is_page_template(‘templates/template-home.php’) ) {
    //
    } elseif ( is_page() && $inner_disable == ‘disable’ ) {
    //
    } elseif (is_singular( array( ‘products’ ) ) ) {
    trav_get_template( ‘inner-3.php’, ‘/templates/inners’ );
    }////got this working
    elseif ( is_page() && is_page_template( ‘templates/custom-page.php’ ) ) {
    trav_get_template( ‘inner-p.php’, ‘/templates/inners’ );////but not getting this result…instead getting the below one for this condition aswell.
    } else {
    trav_get_template( ‘inner-1.php’, ‘/templates/inners’ );
    }

    1. Are you getting an error message?

  2. Northwesterner May 27, 2019 at 7:13 pm

    Except that teaching: { ?> with the brace located inside the php closing tag is improper syntax. Refer to WP or PHP code tuts.

  3. Martins Toritseju April 27, 2019 at 11:13 pm

    This is a really nice tutorial. It helped me a lot in creating my child theme.

    1. Hey Martin, glad it helped. Do follow us on Facebook and Twitter for more tutorials. 🙂

  4. Henrique Silvério June 4, 2014 at 1:47 pm

    Nice tips. Understanding how to use conditional tags is a key requisite to WordPress developers. 🙂

  5. Nebulas Website Design May 22, 2014 at 6:16 am

    Thanks for this have got to do some work on a client’s website so a link only appears in a certain shop category so this will come in handy thank you.

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!