1

In my custom post type (portfolio_pt) I have some hierarchical category structure something like that:

ParentCat
|-child
    |-someItem
ParentCat2
|-cild
...

When I try to get list of all terms by get_the_terms function for "someItem" post this function return only last category ("Child Cat" without "Parent Cat") example:

global $post;
$terms = get_the_terms( $post->ID, 'portfolio_pt_category' );

I try to use get_the_term_list function but effect is the same, it returns only last category.

Thanks for help!

2
  • How are you attempting to output $terms?
    – s_ha_dum
    Commented Apr 25, 2014 at 13:05
  • Just print_r the array and I have only last category in array Array ( [9] => stdClass Object ( [term_id] => 9 [name] => Webdesign [slug] => webdesign [term_group] => 0 [term_taxonomy_id] => 9 [taxonomy] => portfolio_pt_category [description] => with Parent [parent] => 27 [count] => 7 [object_id] => 632 [filter] => raw ) ) Commented Apr 25, 2014 at 13:15

1 Answer 1

2

Instead of get_the_terms try using get_terms and set the hierarchal argument to true. Also make sure you show empty so you are not hiding your empty categories.

Try something like this:

$args = array(
'hide_empty'    => false,
'hierarchical'    => true
); 
$terms = get_terms('portfolio_pt', $args);

foreach($terms as $term)
    echo $term->name;
3
  • this return all taxonomys but I need only for directed post. Commented Apr 26, 2014 at 18:27
  • this does not return all taxonomies. it will only return the portfolio_pt taxonomy.
    – campatsky
    Commented Apr 30, 2014 at 14:36
  • yes but for all posts not only for directed post_id Commented May 5, 2014 at 16:21

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.