I need to find a way to list all categories – empty or not – in a hierarchial list – like wp_list_categories – also showing the slug, cat name and a link to edit in the admin.
Here is what I have so far:
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => '0',
);
$categories = get_categories($args);
foreach( $categories as $category ) {
$cat_ID = $category->term_id;
$cat_name = $category->name;
#$cat_desc = $category->description; if ( !$cat_desc { $cat_desc = 'Nada!' } );
$cat_count = $category->count;
echo '<p><strong>'.$cat_name.'</strong>';
echo ' / <a href="' . get_category_link( $cat_ID ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat_name ) . '" ' . '>View ( '. $cat_count . ' posts )</a> ';
#echo ' / Desc: '. $cat_desc . '';
echo ' / <a href="'. get_admin_url().'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$cat_ID.'&post_type=post" title="Edit Category">Edit</a>';
echo '</p>';
}
All is good, but not nicely ordered – just an alphabetical list.
output as unordered list:
A slightly updated version of Michaelâs answer to use the more generic get_terms (so you can get custom taxonomies, in this case I wanted the WooCommerce product category taxonomy of
product_cat
).Simplified a little to take out the edit link etc. You can add those as required.
You can use following code:
Hmm I think you need to include
'hierarchical' => 1,
in your args list. Also you have one comma too much at the end of the args list. After the last argument you do not need a comma 🙂Here’s a complete example: