Loop through taxonomy parent, and child terms

Support MB Views Loop through taxonomy parent, and child termsResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #20319
    JasonJason
    Participant

    In the code below I'm trying to loop through the parent terms. And display the subsequent child terms. So, in this example the parent term is the State, and the child terms are the Cities in that state. The first function, mb.get_terms(), is working correctly. It returns the correct list of parent terms. However, the second function, mb.get_term_children(), does not return the child terms. Think the term.ID is not present, and that is causing this function to not work properly.

    
        <section id="home-service-areas" class="pv4">
            <h2 class="tc">Service Areas</h2>
            <div class="flex-ns justify-center items-center center mw9 ph3">
                {% set args = { taxonomy: 'service-area', parent: 0 } %}
                {% set terms = mb.get_terms( args ) %}
                {% for key, term in terms %}
                <div class="w-25-ns mh4 mv4">
                    <h3>{{ term.name }}</h3>
                    <ul>
                        {% set termchildren = mb.get_term_children( term.ID, 'service-area' ) %}
                        {% for key, child in termchildren %}
                        <li>{{ child.name }}</li>
                        {% endfor %}
                    </ul>
                </div>
                {% endfor %}
            </div>
        </section>
    
    #20324
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The property term ID of the object WP_Term is term_id, you can change the code to term.term_id. And the function get_term_childrent() returns an array of child term IDs so you should follow a basic example to get the child term name.

    For more information, please follow the documentation.
    https://developer.wordpress.org/reference/classes/wp_term/
    https://developer.wordpress.org/reference/functions/get_term_children/

    #20331
    JasonJason
    Participant

    This worked! Thank you very much!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.