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!

  • How are you attempting to output $terms? – s_ha_dum Apr 25 '14 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 ) ) – Michał Kalkowski Apr 25 '14 at 13:15
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;
  • this return all taxonomys but I need only for directed post. – Michał Kalkowski Apr 26 '14 at 18:27
  • this does not return all taxonomies. it will only return the portfolio_pt taxonomy. – campatsky Apr 30 '14 at 14:36
  • yes but for all posts not only for directed post_id – Michał Kalkowski May 5 '14 at 16:21

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.