我建议如下:
function exclude_widget_subcategories($args){
$all_categories = get_terms(\'category\', array(\'parent\' => 0, \'fields\' => \'ids\'));
$exclude_categories = array();
foreach($all_categories as $category_id){
$children = get_term_children($category_id, \'category\');
if(count($children)!=0){
$exclude_categories[] = $category_id;
}
}
$exclude = implode(",",$exclude_categories); // The IDs of the excluding categories
$args["exclude"] = $exclude;
return $args;
}
add_filter("widget_categories_args","exclude_widget_subcategories");
get_terms(\'category\', array(\'parent\' => 0, \'fields\' => \'ids\'));
获取属于“0”子级的所有类别wordpress术语,即它们是顶级项。它还指定只返回id,而不是term对象。更多文档
hereget_term_children
虽然需要注意的是,您需要将分类法的名称传递给函数调用,但这应该是不言自明的。更多文档here
请注意,我尚未对此进行测试,因此可能存在语法错误-如果有,请告诉我,我可以进一步提供帮助:)