0

I have successfully listed the terms and sub-terms for my custom taxonomies, but the terms are out of order. With the current code, the list shows up as:

Category1
Category2
SubCat2
SubCategory1
SubSubCat2
SubSubCategory1

What am I missing to get them to show up like this:

Category1  
  SubCategory1  
    SubSubCatagory1  
Category2  
   SubCat2  
    SubSubCat2

My Code:

<?php  $args=array('public'   => true, '_builtin' => false); 
$output = 'names';
$operator = 'and';
$taxonomies=get_taxonomies($args,$output,$operator); 
if  ($taxonomies) {
    foreach ($taxonomies  as $taxonomy ) {
        $terms = get_terms($taxonomy);
        $count = count($terms);
        if ( $count > 0 ){
            echo '<ul>';
            foreach ( $terms as $term ) {
                $termlinks= get_term_link($term,$taxonomy);
                ?> <a href="<?php echo $termlinks; ?>">
                <?php echo "<li>" . $term->name . "</li>"; ?></a><?php
            }
        echo "</ul>";
        }
    }
}
?>

Hoping someone can help me out! :)

0

Use wp_list_categories() instead, which handles the display and ordering of hierarchical terms.

Despite its name suggesting that it's just for categories, it works with custom taxonomies too:

$taxonomy_args = array( 
    'public'   => true,
    '_builtin' => false,
); 
$output = 'names';
$operator = 'and';
$taxonomies = get_taxonomies( $taxonomy_args, $output, $operator ); 

if ( $taxonomies ) {
    foreach ( $taxonomies as $taxonomy ) {
        echo '<ul>';

        wp_list_categories( array (
            'order'    => 'ASC',
            'orderby'  => 'name',
            'title_li' => '',
            'taxonomy' => $taxonomy,
            'show_option_none' => '',
        ) );

        echo '</ul>';
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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