1

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:

<?php  
$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>
       <?php  
  }
?>      

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')

3 Answers 3

1

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";
    endforeach;
    echo "</ul>\n";
  endif; // ( ! empty( $child_terms ) )
  echo "</li>\n";
  echo "</ul>\n";
endif; // ( ! empty( $terms ) )
8
  • 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
    Commented 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? Commented Feb 26, 2015 at 13:38
  • I updated the question but it's more or less of what you said
    – wp richard
    Commented 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. Commented Feb 26, 2015 at 15:24
  • It seems to work but the output it's without links to each category, just text :/
    – wp richard
    Commented Feb 26, 2015 at 17:05
0

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;
}
0

I found most of the solutions offered for CPT did not work for Custom Post Types. Mostly because they assume a taxonomy of 'category' So I wrote the following function.

Given the ID of the current ( child) category, it works its way up the chain to give the parent(s) of the category. It returns an array containing url, name, slug and id of parents.

To call it from a taxonmy page ( and others ) I used:

$parent_array = get_category_parent_array($term->term_id, 'my_taxonomy_name');

or

$parent_array =  get_category_parent_array($term->term_id, $term->taxonomy);
/**
 * @param $category_id
 *
 * @return array        Returns either empty array if no parents or
 *                      an array of  categories from base category[0] to parent category
 *                      The array has a sub array of ['id'] ['name'] ['slug'] ['url']
 *
 */

function get_category_parent_array( $category_id, $category_taxonomy = 'category'){

    $category_id = abs((int)$category_id);
    $category_taxonomy = sanitize_key( $category_taxonomy);
    
    // Sanity check for top parent - ( id = 0 )
    if( 0 == $category_id ){
        return array();
    }

    $child_category = get_term( $category_id, $category_taxonomy);
    if( empty($child_category)){
        return array();
    }

    $parent_category_id = $child_category->parent;
    if( 0 == $parent_category_id ){
        return array(); // We cant get category 0.
    }


    // We have checked if the parent_id is 0 but double check here for other issues causing empty parent category
    $parent_category = get_term( $parent_category_id, $category_taxonomy);
    if ( empty ( $parent_category ) ){
        return array();
    }

    // Prepare the return array
    $parent_array = array();

    $parent_array['id'] =   $parent_category_id;
    $parent_array['slug'] = esc_attr($parent_category->slug);
    $parent_array['name'] = esc_attr($parent_category->name);
    $parent_array['url'] =  esc_url(get_term_link( $parent_category));

    if ( 0 == $parent_category_id ){
        return $parent_array;
    }else{
        $new_parent = get_category_parent_array($parent_category_id, $category_taxonomy );
        if(! empty( $new_parent ) ){
            return array( $new_parent, $parent_array);
        }else{
            return $parent_array;
        }

    }

}

For example. In a tree of Home>Products>Platic-Bags>Poly-Bags>3muBags Where the child / current category is 3mu it returned the following:


array (
  0 => 
  array (
    'id' => 260,
    'slug' => 'plastic-bags',
    'name' => 'Plastic Bags',
    'url' => 'https://example.com/product_category/plastic-bags/',
  ),
  1 => 
  array (
    'id' => 261,
    'slug' => 'polybags',
    'name' => 'Polybags',
    'url' => 'https://exmapl.com/product_category/polybags/',
  ),
);

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.