WordPress.org

Forums

How to get term child? (2 posts)

  1. LoLina
    Member
    Posted 3 years ago #

    I have three level categories in my WordPress site. It's looks like:

    Parent A
    - Child A1
    - - Child child A11
    - - Child child A12
    - Child A2
    - - Child child A21
    - - Child child A22
    Parent B
    - Child B1
    - - Child child B11
    - - Child child B12
    - Child B2
    - - Child child B11
    - - Child child B12

    I have a code:

    <?php
            $termID = 3; // Parent A ID
            $taxonomyName = "products";
            $termchildren = get_term_children( $termID, $taxonomyName );
                echo'<div class="sub"> <div class="links">';
                  foreach ($termchildren as $child) {
                      $term = get_term_by( 'id', $child, $taxonomyName );
                      echo'<a href="#">' . $term->name . '</a>';}
                echo'</div> </div>';?>

    The result of the code is:

    Child A1
    Child child A11
    Child child A12
    Child A2
    Child child A21
    Child child A22

    How to get only the Child without Child Child?

  2. vjpo
    Member
    Posted 3 years ago #

    You can try wp_list_categories() with specified 'depth' parameter.
    http://codex.wordpress.org/Template_Tags/wp_list_categories

Topic Closed

This topic has been closed to new replies.

About this Topic