List categories and exclude child categories

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)…

    $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
    $cat_args['title_li'] = '';
    $cat_args['exclude_tree'] = 1;
    wp_list_categories(apply_filters('widget_categories_args', $cat_args));

Related posts

Leave a Reply

2 comments

  1. After some trial and error, this is what it took to make it work…

        $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));
    
  2. $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));
    

    I added the ‘depth’ argument to your array. Check out the Codex Page for full documentation of the function.