1

I have a hierarchical custom taxonomy "company".

Companies are hierarchical, they can have both owners and subsidiaries. So, some of my "company" terms have parents and children. Take this real example of a set of my "company" terms

  • WPP
    • 24/7 realmedia
    • AKQA
    • GroupM
      • Finecast
      • Group M Entertainment
      • Maxus
      • MEC
      • MediaCom
      • Modi Media
      • Real Media Group
      • Xaxis
    • JWT
    • Kantar
      • Kantar Media
      • TNS Media Intelligene
    • Millward Brown
    • Ogilvy
      • Ogilvy Entertainment
    • Possible
    • VML
    • Wavemaker
    • Wunderman

What I'd like to do, on a company term page, is to show the full corporate tree, as above, in order to situate the term in its full hierarchical context.

That goes not just for the top-most term, eg. "WPP", but also a mid-hierarchy child or grand-child, eg. "GroupM" or "Ogilvy Entertainment.

I don't mean all terms in the "company" taxonomy, I mean all terms up and down from the given term, including children of upper terms.

In other words, for any term, spin back up to the top-most parent and display all sub terms properly, in an indented HTML list - preferably with something unique applied to the current term title, eg. no get_term_link applied.

What I have so far is the following...

  <?php
      // Parents
      $parent_args = array(
        'inclusive' => 'false'
      );
      echo get_term_parents_list( $organisation->term_id, 'company', $parent_args );
      ?>


      <?php
      // Children
      $org_children = get_term_children( $organisation->term_id, 'company' ); // Get child terms of this

      echo '<ul class="list-unstyled">';
      foreach ( $org_children as $org_child ) {
        $child_term = get_term_by( 'id', $org_child, 'company' );
        $child_id_prefixed = 'company_'. $child_term->term_id;
                if (empty(get_field( 'domain', $child_id_prefixed ))) {
                    $favicon = 'null';
                } else {
                    $favicon = get_field( 'domain', $child_id_prefixed );
                }

        echo '<li><small><img src="https://www.google.com/s2/favicons?domain='. $favicon.'" width="12" class="mr-2"><a href="' . get_term_link( $org_child, 'company' ) . '">' . $child_term->name . '</a></small></li>';
      }
      echo '</ul>';
      ?>

Which produces this...

enter image description here

However, this:

  1. Is a two-step process, in which I get parents and then children (I'd prefer a singular process).
  2. The output is not an indented list.

So, how to show all possible parents and children of a hierarchical taxonomy term?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.