'WordPress get terms and orderby
I am querying the terms in a custom taxonomy i have with the following code:
<?php
$term_slug = get_query_var('term');
$taxonomyName = get_query_var('taxonomy');
$current_term = get_term_by('slug', $term_slug, $taxonomyName);
$termchildren = get_term_children($current_term->term_id, $taxonomyName);
foreach ($termchildren as $child) {
$term = get_term_by('id', $child, $taxonomyName);
echo '<li id="mid"><a href="#tab-' . $slug = $term->slug . '">' . $term->name . '</a></li>';
}
?>
This is all working great! The problem I have is that I would like to order it by menu-order or something similar. At the moment they are in random order!
Can anyone suggest what I need to do?
Solution 1:[1]
Use the code in the taxonomy file:
$wterm = get_queried_object();
$termslug = $wterm->slug;
$termname = $wterm->name;
$termid = $wterm->term_id;
$terms = get_terms( 'taxonomy-name', array(
'orderby' => 'name',
'hide_empty' => 0,
'child_of' => $termid
) );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo $term->name ;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Carlos Volp |
