use*_*143 0 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>
Run Code Online (Sandbox Code Playgroud)
相反,所有列表项都作为父级输出,如下所示......
<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>
Run Code Online (Sandbox Code Playgroud)
……但他们应该是这样的……
<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>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,“hierarchical”设置为 1,但它没有按预期工作。
PS:我无法使用标准的 wp_list_categories 方法(http://codex.wordpress.org/Template_Tags/wp_list_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 ) {
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>
Run Code Online (Sandbox Code Playgroud)
更新
$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 );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8466 次 |
| 最近记录: |