Wordpress类别层次结构

时间:2014-12-01 02:21:14

标签: wordpress hierarchy categories

我似乎无法找出为什么此代码不会输出层次结构中的类别:

<ul>
<?php  
$args = array(
    'show_option_all'    => '',
    'container'           => false, 
    'orderby'            => 'name',
    'order'              => 'ASC',
    'hide_empty'         => 0,
    'use_desc_for_title' => 0,
    'child_of'           => 0,
    'hierarchical'       => 1,
    'number'             => null,
    'echo'               => 1,
    'depth'              => -1,
    'taxonomy'           => 'category'

); 
$categories = get_categories( $args );
foreach ( $categories as $category ) {
    echo '<li><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '(' . $category->count . ')</a></li>';
}
?>
</ul>

相反,所有列表项都像父母一样输出......

<ul>
    <li><a href="http://test.dev/category/media/">Media(1)</a></li>
    <li><a href="http://test.dev/category/media/movies/">Movies(1)</a></li>
    <li><a href="http://test.dev/category/media/tv-shows/">TV Shows(1)</a></li>
    <li><a href="http://test.dev/category/uncategorised/">Uncategorised(1)</a></li>
</ul>

......但他们应该是这样......

<ul>
    <li><a href="http://test.dev/category/media/">Media(1)</a>
        <ul>
          <li><a href="http://test.dev/category/media/movies/">Movies(1)</a></li>
          <li><a href="http://test.dev/category/media/tv-shows/">TV Shows(1)</a></li>
        </ul>
    </li>
    <li><a href="http://wordpress.dev/category/uncategorised/">Uncategorised(1)</a></li>
</ul>

正如您所见,'hierarchical'设置为1,但它不能按预期工作。

PS:我不能使用标准的wp_list_categories方法(http://codex.wordpress.org/Template_Tags/wp_list_categories),因为我需要能够自定义列表中的标记。

任何建议都会有所帮助。

2 个答案:

答案 0 :(得分:3)

您可以使用以下代码:

<ul>
<?php  
$args = array(
    'show_option_all'    => '',
    'container'           => false,
    'orderby'            => 'name',
    'order'              => 'ASC',
    'hide_empty'         => 0,
    'use_desc_for_title' => 0,
    'child_of'           => 0,
    'hierarchical'       => 1,
    'number'             => null,
    'echo'               => 1,
    'depth'              => -1,
    'taxonomy'           => 'category'

);
$categories = get_categories( $args );  
foreach ( $categories as $category ) {  
    if($category->parent==0)
    echo '<li><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '(' . $category->count . ')</a></li>';
    else
    echo '<ul><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '(' . $category->count . ')</a></li></ul>';
}
?>
</ul>

<强>更新

$args = array(

    'hide_empty'         => 0,
    'echo'               => 1,
    'taxonomy'           => 'category',
    'hierarchical'  =>1,
    'show_count' => 1,

);

function add_class_wp_list_categories($wp_list_categories) {
        $pattern = '/<li class="/is';
        $replacement = '<li class="first ';
        return preg_replace($pattern, $replacement, $wp_list_categories);
}
add_filter('wp_list_categories','add_class_wp_list_categories');

echo wp_list_categories( $args ); 

答案 1 :(得分:0)

使用 'orderby' => 'parent'

$args = array(
    'taxonomy' => 'your_tax',
    'orderby' => 'parent'
);
$cats = get_categories($args);