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.

I am using this code to fetch a list of child taxonomies. I want the order displayed to be alphabetical, but it is not! Any help in figuring out how to do it? I guess ordering it before returning it will be most optimal?

thanks

<?php
$termID = 4;
$taxonomyName = "location";
$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
    
[ this answer for a related problem][1] [1]: wordpress.stackexchange.com/questions/17145/… –  Mubbashar Nov 20 '12 at 13:39

1 Answer 1

If you use the more generic get_terms you can include in the $args field all of the information to get the term or the children and specifically say what you want to order by and how you want it to be ordered.

Ex.

$termchildren = get_terms( "location",
                             array(
                                   'child_of' => 4,
                                   'orderby' => 'name',
                                   'order' => 'ASC'
                                  )
                         );

child_of: Get all descendents of this term. Default is 0. Make sure you use an integer value. Like 1, 2, 3, 4 and not '1', '2', '3', '4'.

parent: Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned. Default is an empty string.

share|improve this answer
    
thanks for the code but when I sub in your string for $termchildren nothing loads in at all. Any idea why it won't work in context of my code example above? –  Redwall Nov 21 '12 at 8:01
    
The child_of argument expects an integer, not a string. Try substituting '4' with 4. –  David Gard Aug 8 '13 at 8:14
    
Sorry about that, updated the code. Thanks David. –  Freddy Aug 8 '13 at 19:20

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.