1

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.

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?

  • Try using is_tax('taxonomy', 'term') - View Codex – Howdy_McGee Mar 27 '14 at 14:44
  • There's also a space in your "is _category." Should be "is_category". And double-check that slug's right. Did you really put "slug-" in your category slug? – mrwweb Mar 27 '14 at 15:30
2

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'
}
  • Still need a semi-colon after that echo line. ;-) – heytricia Jun 23 '17 at 20:07
-1

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.

-2

Refer to this codex for detailed understanding: https://codex.wordpress.org/Function_Reference/get_categories

You can do something like this:

        $args = array(
                'taxonomy'     => **$your_taxonomy**,
                'orderby'      => name,
                'title_li'     => $title,
        );
        $sub_cats = get_categories( $args );
        if( !empty($sub_cats) ) {
         // echo our stuff
        }
  • 1
    You are completely on the wrong road here. I would love to see some kind of explanation that explains why you would want to use get_categories – Pieter Goosen Jan 7 '16 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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