How to Use WordPress Conditional Tags

Share this on:

WordPress Conditional Tags


WordPress has conditional tags which can be used to change the content shown under certain conditions or on certain pages or posts. This tutorial will walk you through some examples, so that the next time you encounter a conditional statement, you will understand it. Conditional tags are usually found in theme template files or functions.php. Some examples will apply only to themes built for the Genesis Framework.

Some Common Conditional Tags and What They Mean

is_home() – This refers either to your Home page or to the Blog page, depending on your theme.
is_front_page() – This refers to your Home page depending on theme and your Settings > Reading.
is_single() – This one refers to any single Post, but not a single Page
is_page() – This refers to any Page.
is_category() – This refers to any Category archive page.

Referring to a Particular Post, Page or Category

Sometimes you may want to just apply the conditions to a particular post, page, or category. Then you can reference the page or category by slug or by ID. (Learn to find post ID here.) The slug is just the part of the page URL that references the page name. So if your page is “About Me”, your page URL may be “http://mysite.com/about-me/”, then the page slug is “about-me”.

To reference one page, by the page slug:
is_page( 'about-me' )
To reference one page, by the page ID:
is_page( 456 )
To reference one post, by the post slug:
is_single( 'write-about-me-page' )
To reference one post, by the post ID:
is_single( 3924 )
To reference one category, by the category slug:
is_category( 'how-to-blog' )
To reference one category, by the category ID:
is_category( 5 )

Referring to Several Posts, Pages or Categories

If you want to refer to more than one post, page, or category, it is similar, but the slugs or IDs are listed as an array.
To reference several pages, by the page slug:
is_page( array( 'about-me', 'my-services', 'contact-me' ) )
To reference several pages, by the page ID:
is_page( array( 456, 534, 679 ) )
It would be similar for several single posts or categories.

Using AND and OR

Now suppose we want to refer to more than one conditional tag – the home page and three other pages – About Me page, My Services page, and Contact Me page.
We would connect two conditional tags: is_home(), is_page() – with AND or use &&.

is_home() && is_page( array( 'about-me', 'my-services', 'contact-me' ) )

If we wanted to refer to the home page or a category, we could connect two conditionals with ‘OR’ or use ‘||’.

is_home() || is_category( array( 5, 6, 7 ) )

Quick Look at Functions

Conditionals are often used in functions that look like this:

<?php //Do not add this line
function my_function_name() {
     if ( is_home() || is_category( array( 5, 6, 7 ) ) ) {
          /* Do something here */
     }
}

The function name is my_function_name. Next we have the conditional as an if-statement that uses the conditional tags is_home() OR is_category(). Then our function is going to do something. In addition, we will need a statement to call the function.

Remove Entry Title on Pages – Genesis Only

Let’s say you want to remove the page title from just a few pages in your Genesis theme. The page slugs are ‘about-me’, ‘my-services’, and ‘contact-me’.

The Genesis action hook to remove the title is:

remove_action('genesis_entry_header', 'genesis_do_post_title');

You can find more hooks like this in the Code Snippets page when you are logged into My StudioPress.

So the conditional to remove the title for only those three pages, would be:

<?php //Do not add this line
if ( is_page( array( 'about-me', 'my-services', 'contact-me' ) ) ) {
     remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
}

But in order for our theme to actually use that conditional, we have to create a function in our functions.php file, and call that function.
So let’s create the function, and name it “awd_remove_page_title”.

<?php //Do not add this line
function awd_remove_page_title() {
     if ( is_page( array( 'about-me', 'my-services', 'contact-me' ) ) ) {
          remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
     }
} 

Now we can hook it to “genesis_entry_header” (act on the function).
The complete code to remove the page title on those three pages will be can be added to your functions.php.

<?php //Do not add this line
// Remove the title from pages About Me, My Services, and Contact Me
add_action( 'genesis_entry_header', 'awd_remove_page_title', 5 );
function awd_remove_page_title() {
     if ( is_page( array( 'about-me', 'my-services', 'contact-me' ) ) ) {
          remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
     }
} 

The “5” is a priority code, which helps WordPress execute code in the desired order.

Genesis adds HTML markup around the title, and if you also wanted to remove the markup, you could use this in your functions.php:

<?php //Do not add this line
// Remove the title and markup from pages About Me, My Services, and Contact Me
add_action( 'genesis_entry_header', 'awd_remove_page_title', 4 );
function awd_remove_page_title() {
     if ( is_page( array( 'about-me', 'my-services', 'contact-me' ) ) ) {
          remove_action('genesis_entry_header', 'genesis_entry_header_markup_open', 5 );
          remove_action('genesis_entry_header', 'genesis_do_post_title');
          remove_action('genesis_entry_header', 'genesis_entry_header_markup_close', 15 ); 
     }
}

Add a Body Class for Posts in Only Two Categories

Now let’s suppose we want to style posts in two categories – Category 1 and Category 2 – a bit differently from the other categories. We can add the same body class to these posts to make styling easier. This can be used in any WordPress theme.
The conditional should refer to single posts that are “in_category()”, which is another conditional tag:

if ( is_single() && in_category( array( 'category-1', 'category-2' ) ) )
<?php //Do not add this line
// Add specific CSS body class
add_filter( 'body_class', 'awd_add_body_class' );
function awd_add_body_class( $classes ) {
    if ( is_single() && in_category( array( 'category-1', 'category-2' ) ) ) {
        $classes[] = 'special-category';
        return $classes;
    }
}

This time you’re calling the function awd_add_body_class with a filter, instead of an action hook. You are assigning the class special-category to the body element of your posts in Category 1 and Category 2. Then you can style those posts with a dotted border by adding the following to your style.css:

.special-category .entry {
    border: 2px dotted #ccc;
}

Or maybe you would like to add some styles to these special-category posts using Dashicons.

You can have a beautiful, hardworking website for your small business.

Tell me about your website project for a personalized solution!


Do you need website tips?

Sign up to get easy-to-use WordPress tutorials, Genesis theme customizations, and other helpful tips for your small business website.

Your email address will be used to send you blog posts. Privacy Policy


photo of Marcy Diaz who owns Amethyst Website Design

Comments

7 responses to “How to Use WordPress Conditional Tags”

  1. Marcus Tibesar Avatar
    Marcus Tibesar

    One of the most useful, comprehensive Posts I’ve read in a long time. Great job Marcy explaining this complex php coding in laymen’s terms.

    1. Marcy Diaz Avatar

      Thank you, Marcus! I’m glad it was helpful.

  2. Sara Avatar
    Sara

    Fantastic post thanks so much for sharing.

    1. Marcy Diaz Avatar

      Thanks, Sara! You inspired me. :)

  3. Tip Sobat Avatar
    Tip Sobat

    Hi Marcy,
    How to show it only on home (blog type not frontpage), and hide it on pagination of home ?

    1. Marcy Diaz Avatar

      You could use a conditional something like this:
      if ( get_query_var( 'paged' ) < 2 )

  4. Nuno Morais Sarmento Avatar

    Thank you very much for sharing this, awesome information well written.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.