1

Let's say we have a taxonomy that goes three levels deep, eg. A > B > C.

Next, we create a post and assign it to the "C" term.
In our case here, there will only ever be a single assignment; it's the event a thing took place at.

On the post's page, I want to output not just the assigned term, but the hierchical path to it. So, even though the post is assigned only to "C," I want to display "A > B > C" with links to the archive page for each of those terms along the way.

get_the_term_list won't do it, and unless I've been using very bad search terms for an entire week, this is apparently very hard to do and every supposed solution I've managed to find is broken in some way. (One seriously required assumptions about the order of the taxonomy terms' database ID values.)
Here's one example that seemed to be addressing my exact case. That…simply outputs nothing in my installation, and var_dump($links_string) produces something like "string(0) "";" (Note there's two spots where "---" appears that should be collapsed to a single dash.)

Am I missing something?

Note things I don't want:

  • To manually assign every term down the hierarchy. It's clearly not necessary. If you visit the archive page for "A" in the example structure, WordPress happily lists the posts in every sub-term, so obviously it has some way to see the hierarchy.
    • If you've ever had clients, you know that this will just not get done.
  • Breadcrumbs. These all start by listing the site root, and end with the post title, and are extremely difficult to clean up. The Yoast SEO implementation, for example, can be tricked into removing the root with blank data, and the post title with an external filter, but it's impossible to not end up with a trailing item divider.
    • Also, every breadcrumb implementation I've found for some reason locks itself to providing a single function that displays a single configured taxonomy path (to the post). I have lots of taxonomies, and may eventually need to list other ones in a similar way, so need a generic function that lets me tell it what taxonomy to use.
2
  • What else have you already tried? Did you try to do this in the Permalink Settings? If not try to add a custom structure, using: /%taxonomy_name%/%postname%/ or whatever would be the three levels deep equivalent. It might also help to know if you're using custom post types or not Mar 23, 2016 at 21:22
  • This is unrelated to permalinks. I'm looking for a templating function.
    – Su'
    Mar 24, 2016 at 0:57

1 Answer 1

2

Use get_the_terms to fetch the post's term, then use get_ancestors to get an array of that term's parent IDs.

$tax = 'category';
$terms = get_the_terms( get_the_ID(), $tax );
if( $terms && ! is_wp_error( $terms ) ){
    // check for and output any ancestors
    $ancestors = array_reverse(get_ancestors( $terms[0]->term_id, $tax ));
    if( $ancestors ){
        foreach( $ancestors as $ancestor ){
            $parent = get_term( $ancestor, $tax );
            if( $parent && ! is_wp_error( $parent ) ){
                echo $parent->name;
                echo get_term_link( $parent );
            }
        }
    }
    // output directly assigned term
    echo $terms[0]->name;
    echo get_term_link( $terms[0] );
}
1
  • Well, that's much simpler (and it works) than almost everything else I'd seen; wonder what all those people were thinking. One small edit, though, since get_ancestors crawls in the wrong direction for our needs. Thanks.
    – Su'
    Mar 25, 2016 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.