Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do i get output of this in alphabetical order

<?php
    $termID = 5;
    $taxonomyName = 'area';
    $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>';
?>
share|improve this question
add comment

1 Answer 1

up vote 7 down vote accepted

get_term_children() only outputs the term IDs, and you later get details for each term using get_term_by(). You can combine these queries in one using get_terms() with the child_of argument:

get_terms( $taxonomyName, array( 'child_of' => $termID ) );

By default this sorts by name. However, it is possible that the child_of argument undoes the sorting. In that case you can sort it again using usort(). See an example at this answer for a related problem.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.