0

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

6
  • 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. Commented Oct 25, 2013 at 8:25
  • i used custom post type creator. By which it created default categories
    – luckyrajiv
    Commented Oct 25, 2013 at 8:28
  • and where are you using the above set of codes? Commented Oct 25, 2013 at 8:36
  • in single-listing.php
    – luckyrajiv
    Commented Oct 25, 2013 at 9:07
  • can you update your code with whole code from that file Commented Oct 25, 2013 at 9:57

3 Answers 3

1

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}?>
0
         <?php if( has_term( 'music', 'listingcategory' ) ) {
                        //content1
                        }
                        else
                        {
                        //content2
                        }
         ?>

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

0

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
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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