views:

20

answers:

2

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));
A: 

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));
JapanPro
Thanks JapanPro, but that doesn't work for me. I found the answer through some trial and error. About to post it as an update now.
Scott B
it depend on your root category id , generally its 0, but some time is changed , in that case you have to change as per that. if you see documentation http://codex.wordpress.org/Template_Tags/wp_list_categories your will find meaning of "child-of" and "depth". as combination you can get root categories.
JapanPro
A: 

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));
Scott B
exlude_tree and exlude would appear to be redundant, but in my case, I had to use them both to make this work.
Scott B