WordPress > wp_list_categories with no child categories listed

淺唱寂寞╮ 提交于 2019-12-10 17:53:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!