0

I am having extreme difficulties retrieving the name (or the ID) of the current category of the custom post being viewed (within the loop).

The current category name is "school" and it is a subcategory of the custom category called "category_news"

I am trying the following, but it returns empty.

$terms = get_the_terms( $post->ID , 'category_news' );
if ( $terms != null ){
    foreach( $terms as $term ) {
        print $term->slug ;
        unset($term);
    }
}
  • category_news is your taxonomy? – Rakhi Nov 28 '14 at 11:49
  • yes indeed it is the taxonomy name - created with CPT UI plugin, it also has 3 subcategories – alemur Nov 28 '14 at 12:08
1
get_the_terms( $id, $taxonomy ); 

second parameter should be your taxonomy name instead of category name

| improve this answer | |
  • Mmh my taxonomy name is "category_news" – alemur Nov 28 '14 at 12:06
  • I was writing it wrong, it is 'categories-news' :) thanks – alemur Nov 28 '14 at 12:17

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.