I'm trying to display all the categories that belong to the custom post type that the user is in.

For example this post: http://bit.ly/1ANisxN Is a custom post type that belongs to this page category: http://bit.ly/1FYscKh

As you can see the page category shows it's child categories which is exactly what I want to achieve it the custom post: i.imgur.com/vk4K31T.png

This is the things I have tried in the custom post to show the parent categories with no luck:

<?php echo get_category_parents() ?>

By the way, this is how the category page is echoing its subcategories:

$terms = get_terms( 'btp_work_category' ); 
   foreach ( $terms as $term ) {
       <li><a href="#" data-filter=".filter-<?php echo $term->term_id; ?>"><?php echo $term->name; ?></a></li>

Any idea on how to do this?

For example this category structure:

Electronics (Top category)

  • Cameras (sub category)

    • posts
  • TV (sub category)

    • posts
  • Cellphones (sub category)

    • posts

So If I'm on a cameras post I want to display the top category of it and its subcategories (which are relative to cameras since all are under `Electronics')

2 Answers 2


If you're in the single custom post template, you can get the terms that the post belongs to by use of

$terms = get_the_terms( get_the_ID(), 'btp_work_category' );

Then you need to determine parent term and display it with its children.

The code below assumes that the post belongs to one top category (term) and the taxonomy tree has no more than 2 levels.

$terms = get_the_terms( get_the_ID(), 'btp_work_category' );
if ( ! empty( $terms ) ) :
  echo "<ul>\n";
  // Parent term detection and display
  $term = array_pop( $terms );
  $parent_term = ( $term->parent ? get_term( $term->parent, 'btp_work_category' ) : $term );
  echo "<li>\n";
  echo '<a href="' . get_term_link( $parent_term, 'btp_work_category' ) . '">' . $parent_term->name . '</a>';
  // Getting children
  $child_terms = get_term_children( $parent_term->term_id, 'btp_work_category' );
  if ( ! empty( $child_terms ) ) :
    echo "\n<ul>\n";
    foreach ( $child_terms as $child_term_id ) :
      $child_term = get_term_by( 'id', $child_term_id, 'btp_work_category' );
      echo '<li><a href="' . get_term_link( $child_term, 'btp_work_category' ) . '">' . $child_term->name . "</a></li>\n";
    echo "</ul>\n";
  endif; // ( ! empty( $child_terms ) )
  echo "</li>\n";
  echo "</ul>\n";
endif; // ( ! empty( $terms ) )
  • Hi @macemmek. I want to show in the post, its actual parent category and its subcategories as it is shown in the category page bit.ly/1FYscKh
    – wp richard
    Feb 26, 2015 at 12:56
  • OK, so you don't want to show subcategories that the post belongs to but only the top category that the post belongs to and subcategories of its category, even if the post does not belong to any of these subcategories? Feb 26, 2015 at 13:38
  • I updated the question but it's more or less of what you said
    – wp richard
    Feb 26, 2015 at 14:48
  • Thanks, I've updated my answer. Important: it assumes that the post belongs to one top term and this is a 2-level tree. Feb 26, 2015 at 15:24
  • It seems to work but the output it's without links to each category, just text :/
    – wp richard
    Feb 26, 2015 at 17:05

Here's something that should kinda work... in spite of the fact that CPTs don't have a parent category as regular WP posts do.

function get_cpt_parent_cat_aka_taxonomy() {
    global $post;
    $parent_tax_name = 'Undefined parent taxonomy!';
    $obj_taxonomies = get_object_taxonomies( $post->post_type, 'objects' );
    $slugs = array();
    if( !empty( $obj_taxonomies ) ) {
        foreach ( $obj_taxonomies as $key => $tax ) {
            if( $tax->show_ui === true && $tax->public === true && $tax->hierarchical !== false ) {
                array_push( $slugs, $tax->name );
        $terms = wp_get_post_terms( $post->ID, $slugs );
        $term = get_term( $terms[ 0 ]->term_id, $terms[ 0 ]->taxonomy );
        $parent_tax_name = $term->name;
    return $parent_tax_name;

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.