自定义分类术语链接无序

时间:2016-10-09 作者:moneycode

我已经成功列出了自定义分类法的术语和子术语,但这些术语的顺序不对。使用当前代码,列表显示为:

Category1
Category2
SubCat2
SubCategory1
SubSubCat2
SubSubCategory1
我错过了什么让他们像这样出现:

Category1  
  SubCategory1  
    SubSubCatagory1  
Category2  
   SubCat2  
    SubSubCat2
我的代码:

<?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>";
        }
    }
}
?>
希望有人能帮我!:)

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

使用wp_list_categories() 相反,它处理层次术语的显示和排序。

尽管它的名字表明它只是用于类别,但它也适用于自定义分类法:

$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>\';
    }
}

相关推荐