WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created Custom Post Type called - listing

Also created custom taxonomy - listingcategories

In listing categories, i have created two categories

  1. music 2. features

Now i want to create a conditional statement for music category

I am trying this, but its not working

<?php global $post;
if (($post->post_type == 'listing') && is_category('music')) { 
} ?>

Am i using it right is_category() ?

Please Help.

Thanks Rajiv

share|improve this question
It seems it should work, but can you also update the code you have used to register the custom post type and the custom taxonomy. – Maruti Mohanty Oct 25 '13 at 8:25
i used custom post type creator. By which it created default categories – jamesrotcher Oct 25 '13 at 8:28
and where are you using the above set of codes? – Maruti Mohanty Oct 25 '13 at 8:36
in single-listing.php – jamesrotcher Oct 25 '13 at 9:07
can you update your code with whole code from that file – Maruti Mohanty Oct 25 '13 at 9:57

Cheers guy's this helped me a lot, I used the below which works great, any improvements appreciated.

          $cat1 = array('category1', 'category2');

           if( has_term( $cat1, 'my_custom_post_type' ) ) {
                  //do something
         } else { //do somthing else}?>
share|improve this answer
up vote 0 down vote accepted
         <?php if( has_term( 'music', 'listingcategory' ) ) {

Got working using this http://codex.wordpress.org/Function_Reference/has_term

share|improve this answer

The is_category() conditional applies to a specific taxonomy: category. The category taxonomy, by default, is only registered for the post post-type.

You're using a custom post type - listing - and a custom taxonomy - listingcategories. As such, you will not be able to use is_category() to construct conditional statements for them.

You can use one of the following, depending on your needs:

  • is_tax( $taxonomy, $term ): returns true if the current page is a taxonomy archive index:

    is_tax( 'listingcategories', 'music' )
  • has_term( $taxonomy, $term ): returns true if the current post has the specified taxonomy term applied:

    has_term( 'listingcategories', 'music' )

So, the analog to your original code would be:

<?php global $post;
if ( ($post->post_type == 'listing') && is_tax( 'listingcategories', 'music' ) ) { 
} ?>

And cleaned up using get_post_type():

if ( 'listingcategories' == get_post_type() && is_tax( 'listingcategories', 'music' ) ) {
    // statement
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.