最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to show all possible parents and children of a hierarchical taxonomy term?

matteradmin7PV0评论

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="='. $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...

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?

Post a comment

comment list (0)

  1. No comments so far