0

I am trying to get the category name of a custom post type category in my single-[cpt].php file.

Here is the code I am using:

$queried_object = get_queried_object();
$term_name = $queried_object->name;
echo $term_name;

The category is a sub category. This code just displays the name of the parent category, where I need the name of the category.

1 Answer 1

0

Not sure in what form you need the term/s, but here's a piece of code that will first get all your posts' terms, then print them out as linked names.

<?php
// Get Post Terms
$taxonomy_slug = "your-taxonomy";
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( !empty( $terms ) ) {
  foreach ( $terms as $term ) {
    $out[] = '<a href="'. get_term_link( $term->slug, $taxonomy_slug ) .'">'. $term->name
. "</a>\n";
  }
  echo implode(', ', $out );
}
?>
2
  • Thank you very very much 'The J'. This works Perfectly. I have been playing around with loops for hours with no success. Thanks again
    – aido14
    Commented Oct 26, 2016 at 4:08
  • You're welcome! Remember to tick it as the correct answer, if it's what you were looking for!
    – The J
    Commented Oct 26, 2016 at 4:40

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.