Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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')) { 
//statement
} ?>

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 –  luckyrajiv 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 –  luckyrajiv 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
show 1 more comment

3 Answers

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

    <?php 
          $cat1 = array('category1', 'category2');

           if( has_term( $cat1, 'my_custom_post_type' ) ) {
                  //do something
         } else { //do somthing else}?>
share|improve this answer
add comment
         <?php if( has_term( 'music', 'listingcategory' ) ) {
                        //content1
                        }
                        else
                        {
                        //content2
                        }
         ?>

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

share|improve this answer
add comment

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' ) ) { 
//statement
} ?>

And cleaned up using get_post_type():

<?php
if ( 'listingcategories' == get_post_type() && is_tax( 'listingcategories', 'music' ) ) {
    // statement
}
?>
share|improve this answer
add comment

Your Answer

 
discard

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.