问题
The script below creates a listing of the categories in the site (excluding those in "uncategorized").
If possible, I'd like to modify it so that it only lists the top level categories (no child categories)...
I thought the "depth"=1 argument would do the trick but not so. It lists ALL categories. When I remove the "heirarchical" argyument, it DOES exclude child categories, but then also includes the "uncategorized" category which I'm explicitly excluding via the exclude_tree = 1 argument.
At a loss. WordPress 3.0.1 tested.
$cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
$cat_args['title_li'] = '';
$cat_args['exclude_tree'] = 1;
$cat_args['depth'] = 1;
wp_list_categories(apply_filters('widget_categories_args', $cat_args));
回答1:
add this
$cat_args['child_of'] = 0;
with combination of $cat_args['depth'] = 1;
It will generate o only root category
$cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
$cat_args['title_li'] = '';
$cat_args['exclude_tree'] = 1;
$cat_args['depth'] = 1;
$cat_args['child_of'] = 0;
wp_list_categories(apply_filters('widget_categories_args', $cat_args));
回答2:
After some trial and error, this actually worked for me...
$cat_args = array('orderby' => 'count');
$cat_args['title_li'] = '';
$cat_args['exclude_tree'] = 1;
$cat_args['exclude'] = 1;
$cat_args['depth'] = 1;
wp_list_categories(apply_filters('widget_categories_args', $cat_args));
来源:https://stackoverflow.com/questions/3805107/wordpress-wp-list-categories-with-no-child-categories-listed