WordPress: List custom taxonomy with children

I’m trying to create a list of links for a custom taxonomy along with the children of the terms. i.e:

<ul>
    <li><a href="/telephony">Telephony</a>
        <ul>
            <li><a href="/blackberry">BlackBerry</a></li>
            <li><a href="/fixed-ip">Fixed IP</a></li>
        </ul>
    </li>
    <li><a href="/email">Email</a>
        <ul>
            <li><a href="/fax">Fax</a></li>
            <li><a href="/text">Text</a></li>
            <li><a href="/nhs-email">NHS Email</a></li>
        </ul>
    </li>
</ul>

So far I’ve got:

Read More
    <?php
    $termID = 451;
    $taxonomyName = "service_line_category";
    $termchildren = get_term_children( $termID, $taxonomyName );

    echo '<ul>';
    foreach ($termchildren as $child) {
        $term = get_term_by( 'id', $child, $taxonomyName );
        echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
    }
    echo '</ul>';
    ?>

However this only grabs the children of ‘$termID = 451;’ but I’m not sure how to modify this to get the parents and the children for all of the terms within ‘service_line_category’ in a list.

Any ideas?

Related posts

Leave a Reply

3 comments

  1. Here is the piece of code you need :

    <?php
    $taxonomyName = "service_line_category";
    $terms = get_terms($taxonomyName,array('parent' => 0));
    foreach($terms as $term) {
        echo '<a href="'.get_term_link($term->slug,$taxonomyName).'">'.$term->name.'</a>';
        $term_children = get_term_children($term->term_id,$taxonomyName);
        echo '<ul>';
        foreach($term_children as $term_child_id) {
            $term_child = get_term_by('id',$term_child_id,$taxonomyName);
            echo '<li><a href="' . get_term_link( $term_child->term_id, $taxonomyName ) . '">' . $term_child->name . '</a></li>';
        }
        echo '</ul>';
    }
    ?>
    
  2. @Alexcp so something like this:

        <?php
        $taxonomyName = "service_line_category";
        $termID = get_cat_ID($taxonomyName);
        $termchildren = get_term_children( $termID, $taxonomyName );
    
        echo '<ul>';
        foreach ($termchildren as $child) {
            $term = get_term_by( 'id', $child, $taxonomyName );
            echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
        }
        echo '</ul>';
        ?>
    

    Obviously this doesn’t work but is this along the right lines?