6

嗨,我想在 wordpress 中创建一个类别树,如下所示:

<ul>
    <a href=""> PARENT1 </a>
    <li><a href=""> CHILD 1-1</a></li>
    <li><a href=""> CHILD 1-2</a></li>
     .
     .
     .
</ul>
<ul>
    <a href=""> PARENT2 </a>
    <li><a href=""> CHILD 2-1</a></li>
    <li><a href=""> CHILD 2-2</a></li>
     .
     .
     .
</ul>

我想要以上述格式创建类别列表并仅显示有孩子的类别并隐藏没有孩子的类别

我试过这样的东西,但它没有给我我想要的东西

<?php $args = array(
'type'                     => 'post',
'child_of'                 => 0,
'parent'                   => '',
'orderby'                  => 'name',
'order'                    => 'ASC',
'hide_empty'               => 0,
'hierarchical'             => 1,
'exclude'                  => '',
'include'                  => '',
'number'                   => '',
'taxonomy'                 => 'category',
'pad_counts'               => false 
);
$cats = get_categories( $args );
foreach( $cats as $cat) {
    if($cat->parent == 0) {
        $head = $cat->name;
        $cat_id = $cat->term_id;
    }
    echo "<a href=''>" . $head . "</a>";
    wp_list_cats("sort_column=NAME&optioncount=0&hierarchical=1&hide_empty=0&child_of={$cat_id}");
}

?>

4

4 回答 4

5

(适用于任何分类法,包括“类别”)

$your_taxonomy='category';

function my_Categ_tree($TermName='', $termID, $separator='', $parent_shown=true ){
    $args = 'hierarchical=1&taxonomy='.$TermName.'&hide_empty=0&orderby=id&parent=';
            if ($parent_shown) {$term=get_term($termID , $TermName); $output=$separator.$term->name.'('.$term->term_id.')<br/>'; $parent_shown=false;}
    $separator .= '-';  
    $terms = get_terms($TermName, $args . $termID);
    if(count($terms)>0){
        foreach ($terms as $term) {
            //$selected = ($cat->term_id=="22") ? " selected": "";
            //$output .=  '<option value="'.$category->term_id.'" '.$selected .'>'.$separator.$category->cat_name.'</option>';
            $output .=  $separator.$term->name.'('.$term->term_id.')<br/>';
            $output .=  my_Categ_tree($TermName, $term->term_id, $separator, $parent_shown);
        }
    }
    return $output;
}

然后你可以输出:

1)目标类别(分类)树,使用特定的ID

echo my_Categ_tree($your_taxonomy, 0 );

2) 所有类别/分类法

foreach (get_terms($your_taxonomy, array('hide_empty'=>0, 'parent'=>0)) as $each) {
    echo my_Categ_tree($each->taxonomy,$each->term_id);
}
于 2015-01-21T14:11:03.073 回答
5
<?php
$args = array(
  'taxonomy'     => 'product-type',
  'hierarchical' => true,
  'title_li'     => '',
  'hide_empty'   => false
);
?>

<ul>
<?php wp_list_categories( $args ); ?>
</ul>



You can use the wp_list_categories function also for taxonomies. 

http://codex.wordpress.org/Template_Tags/wp_list_categories

https://wordpress.stackexchange.com/questions/39125/custom-taxonomy-tree-view

于 2013-08-23T11:49:32.603 回答
4

感谢您的回复,我找到了解决方案:此代码运行良好

     $args = array(
            'type'                     => 'post',
            'child_of'                 => 0,
            'parent'                   => 0,
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 0,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'category',
            'pad_counts'               => false 
            );
            $cats = get_categories( $args );
            foreach( $cats as $cat) {
                if($cat->parent == 0) {
                    $parent_cat = null;
                    $head = $cat->name;
                    $head_id = $cat->term_id;
                }
                echo "<ul><a class='parent-category' href=''>" . $head . "</a>";                                                    
                wp_list_cats("sort_column=NAME&optioncount=0&hierarchical=1&hide_empty=0&child_of={$head_id}&show_option_none=");
                echo "</ul>";
            }
于 2013-08-23T18:01:11.827 回答
2

这是一个非常古老的帖子,但也许有一些更干净的代码。对于get_terms的所有选项,请参阅文档。随意使其更通用:

function build_custom_category_tree ($activeCatId, $activeParentId, $parentId = 0) {

    $output = '';
    $terms = get_terms( array(
        'taxonomy' => 'custom_categories',
        'hide_empty' => true,
        'hierarchical' => true,
        'parent' => $parentId
    ) );

    if (count($terms)) {

        $output .= '<ul>';

        foreach ($terms as $term) {
            $output .= '<li class="custom-cat' . ($term->term_id === $activeParentId || $term->term_id === $activeCatId ? ' active' : '') . '">';
            $output .=  $term->name;
            $output .=  build_custom_category_tree($activeCatId, $activeParentId, $term->term_id);
            $output .= '</li>';
        }

        $output .= '</ul>';
    }

    return $output;
}

然后在您的模板中:

<div>
   <?= build_custom_category_tree($catIdOfPost, $rootIdOfCat) ?>
</div>
于 2019-05-27T08:01:51.563 回答