3
    function btp_entry_capture_categories() {
        $out = '';

        global $post;

        $taxonomies = get_object_taxonomies( $post );

        foreach ( $taxonomies as $taxonomy ) {  
            $taxonomy = get_taxonomy( $taxonomy );  
            if ( $taxonomy->query_var && $taxonomy->hierarchical ) {

                $out .= '<div class="entry-categories">';
                    $out .= '<h6>' . $taxonomy->labels->name . '</h6>';
                    $out .= get_the_term_list( $post->ID, $taxonomy->name, '<ul><li>', '</li><li>', ' › </li></ul>' );
                $out .= '</div>';
            }
        }

        return $out;
    }

I'm trying to output the category list ordering by hierarchy, is it possible to do with my code? what would be the best approach to do that?

4
  • What do you mean by "output the category list ordering by hierarchy" Dec 12, 2014 at 17:48
  • Now it outputs like this: Artist, Books, etc (in ASC). But now I want it to get the hierarchy which in this case it would be Books > Artist
    – wpuser
    Dec 12, 2014 at 18:04
  • So you need parent >> child >> grandchild; parent >> child >> grandchild; Dec 12, 2014 at 18:05
  • @PieterGoosen yeah, that's exactly what I want to achieve
    – wpuser
    Dec 12, 2014 at 18:27

1 Answer 1

3

get_the_term_list() will not work here. The best function to use would be wp_get_post_terms()

With the following assumption, this can work

  • If a post only belongs to one parent, one child and/or one grandchild, you can order the terms by term_id.

  • It is widely accepted that the parent will have a lower numbered ID than the child and the child will have a lower numbered ID than the grandchild

With this info, you can get the post terms as follow then inside your code

wp_get_post_terms( $post->ID, $taxonomy->name, array( 'orderby' => 'term_id' ) );

But as I said, you will need to have your post have only one parent, one child and one grandchild in the same tree

EDIT

You can try something like this. You will just need to add the HTML mark-up yourself

function btp_entry_capture_categories() {
    $out = '';

    global $post;

    $taxonomies = get_object_taxonomies( $post );

    foreach ( $taxonomies as $taxonomy ) {  
        $taxonomy = get_taxonomy( $taxonomy );  
        if ( $taxonomy->query_var && $taxonomy->hierarchical ) {

            $out .= '<div class="entry-categories">';
                $out .= '<h6>' . $taxonomy->labels->name . '</h6>';

                $terms = wp_get_post_terms( $post->ID, $taxonomy->name, array( 'orderby' => 'term_id' ) );
                foreach ( $terms as $term ) {

                    $out .= $term->name;

                }
            $out .= '</div>';
        }
    }

    return $out;
}
5
  • thanks Pieter, I'm trying with your code but I get the following error: Catchable fatal error: Object of class WP_Error could not be converted to string
    – wpuser
    Dec 12, 2014 at 18:47
  • Yes, you need to use a foreach loop, wp_get_post_terms returns an object, not a string like get_the_term_list Dec 12, 2014 at 18:49
  • mmm I'm using it, see it in the question? :)
    – wpuser
    Dec 12, 2014 at 18:51
  • I think you misunderstood me there, :-). Check my update Dec 12, 2014 at 18:55
  • 'It is widely accepted that the parent will have a lower numbered ID than the child and the child will have a lower numbered ID than the grandchild' This statement is utterly false, it's completely possible to have parents with lower ids than their children, since they can be assigned after creation, making this answer very inaccurate
    – Tofandel
    Jul 5, 2021 at 15:34

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.