本文介绍了仅获取父类别 wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..我正在尝试创建一个类别列表,但我只想列出父类别而不是子类别.我怎样才能做到这一点?到目前为止,我已经创建了一个列表,其中列出了所有父类和子类.
i'm trying to create a category list, but i only want to list the parent categories and not the child categories. How can i do this? so far i've created a list, which list all parent and child categories.
function categoryList() {
$args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
$output .= '<ul class="category-list">';
foreach($categories as $category) {
if ($category){
$output .= '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
}
}
$output .= '</li>';
$output .= '</ul>';
return $output;
}
推荐答案
对于父类别,我假设您指的是顶级类别.这实际上记录在 get_categories
的 Codex 页面上:您应该使用 parent => 调用
get_categories
0
By parent categories, I assume you mean top-level categories. This is actually documented on the Codex page for get_categories
: You should call get_categories
with parent => 0
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0
);
$categories = get_categories($args);
这篇关于仅获取父类别 wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论