Conditional Statement custom post type category

I would like to display a text depends of my custom taxonomy category, like:

if custom post type category is X, then echo Text Y.

Read More

I’ve tried this, but its not working:

global $post; 
if (($post->post_type == 'myposttype') && is_category('slug-name-of-cat')) {
 echo 'My text'

 }

any ideias?

Related posts

Leave a Reply

3 comments

  1. is_category() does not work on custom taxonomy archive pages. The correct conditional tag here is is_tax() which takes the name of the taxonomy as first parameter and a string|int|array of term name/s, slug/s or ID/s as second parameter.

    So your whole conditional statement can look something like :

    if ( is_tax( 'my_taxonomy', 'slug-name-of-the-term') ) {
        echo 'My text';
    }
    

    EDIT

    In addition, to test whether a post belongs to a specific term, you should use has_term() to test for the specific term

    global $post; 
    if (    ( $post->post_type == 'myposttype' ) 
         && has_term( 'slug-name-of-the_term', 'my_taxonomy' )
    ) {
        echo 'My text';
    }
    
  2. The conditional is_* functions work on query, not on current post. In other words they tell you things about page you are on, however not about the current post.

    From your description I suspect you mean the latter.