正如我在评论中所说的,使用get_categories
, 下面是一些示例性用法。
Code:
$cat_array = get_categories(\'parent=0&hide_empty=0&exclude=1,762,899,951\');
$results_total = count($cat_array);
$cats_per_list = ceil($results_total / 3);
$list_number = 1;
$result_number = 0;
echo \'<ul class="cat_col" id="cat-col-\'.$list_number.\'?>">\';
foreach($cat_array as $category) {
$result_number++;
$category_link = get_category_link( $category->term_id);
if($result_number % $cats_per_list == 0) {
$list_number++;
echo \'<a href="\'.esc_url( $category_link ).\'" title="Tuppersex">\';
echo __( \'Tuppersex\', \'your-text-domain\' ) . \' \' . $category->name.\'</li>\';
echo \'</a>\';
echo \'</ul>
<ul class="cat_col" id="cat-col-\'.$list_number.\'">\';
} else {
echo \'<a href="\'.esc_url( $category_link ).\'" title="Tuppersex">\';
echo __( \'Tuppersex\', \'your-text-domain\' ) . \' \' . $category->name.\'</li>\';
echo \'</a>\';
}
}
Edit:
而不是使用
depth
参数可以使用
parent
值为的参数
0
, 其结果是仅显示顶层。
<小时>
2nd approach:
就像我们意识到深度参数与上述解决方案不起作用一样。在查看源代码后,很明显它不能,因为它被
walk_category_tree()
在里面
wp_list_categories()
. 这一步不是
get_categories
, 因为它不构造任何输出。因此,解决您的问题的另一个合乎逻辑的解决方案是扩展
Walker Class
, 即
Walker_Category
班
Code:
class Custom_Cat_Walker extends Walker_Category {
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
extract($args);
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( \'list_cats\', $cat_name, $category );
$link = \'<a href="\' . esc_url( get_term_link($category) ) . \'" \';
if ( $use_desc_for_title == 0 || empty($category->description) )
$link .= \'title="\' . esc_attr( sprintf(__( \'View all posts filed under %s\' ), $cat_name) ) . \'"\';
else
$link .= \'title="\' . esc_attr( strip_tags( apply_filters( \'category_description\', $category->description, $category ) ) ) . \'"\';
$link .= \'>\';
$link .= __( \'Tuppersex\', \'your-text-domain\' ) . \' \' . $cat_name . \'</a>\';
if ( !empty($feed_image) || !empty($feed) ) {
$link .= \' \';
if ( empty($feed_image) )
$link .= \'(\';
$link .= \'<a href="\' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . \'"\';
if ( empty($feed) ) {
$alt = \' alt="\' . sprintf(__( \'Feed for all posts filed under %s\' ), $cat_name ) . \'"\';
} else {
$title = \' title="\' . $feed . \'"\';
$alt = \' alt="\' . $feed . \'"\';
$name = $feed;
$link .= $title;
}
$link .= \'>\';
if ( empty($feed_image) )
$link .= $name;
else
$link .= "<img src=\'$feed_image\'$alt$title" . \' />\';
$link .= \'</a>\';
if ( empty($feed_image) )
$link .= \')\';
}
if ( !empty($show_count) )
$link .= \' (\' . intval($category->count) . \')\';
if ( \'list\' == $args[\'style\'] ) {
$output .= "\\t<li";
$class = \'cat-item cat-item-\' . $category->term_id;
if ( !empty($current_category) ) {
$_current_category = get_term( $current_category, $category->taxonomy );
if ( $category->term_id == $current_category )
$class .= \' current-cat\';
elseif ( $category->term_id == $_current_category->parent )
$class .= \' current-cat-parent\';
}
$output .= \' class="\' . $class . \'"\';
$output .= ">$link\\n";
} else {
$output .= "\\t$link<br />\\n";
}
}
}
array(
\'title_li\' => \'\',
\'depth\' => 1,
\'walker\' => new Custom_Cat_Walker()
)
wp_list_categories( $args );