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 ) . '" rel="nofollow noreferrer noopener" 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" rel="nofollow noreferrer noopener" title="Edit Category">Edit</a>';
echo '</p>';
}
All is good, but not nicely ordered – just an alphabetical list.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
output as unordered list:
<?php
hierarchical_category_tree( 0 ); // the function call; 0 for all categories; or cat ID
function hierarchical_category_tree( $cat ) {
// wpse-41548 // alchymyth // a hierarchical list of all categories //
$next = get_categories('hide_empty=false&orderby=name&order=ASC&parent=' . $cat);
if( $next ) :
foreach( $next as $cat ) :
echo '<ul><li><strong>' . $cat->name . '</strong>';
echo ' / <a href="' . get_category_link( $cat->term_id ) . '" rel="nofollow noreferrer noopener" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>View ( '. $cat->count . ' posts )</a> ';
echo ' / <a href="'. get_admin_url().'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$cat->term_id.'&post_type=post" rel="nofollow noreferrer noopener" title="Edit Category">Edit</a>';
hierarchical_category_tree( $cat->term_id );
endforeach;
endif;
echo '</li></ul>'; echo "n";
}
?>
Method 2
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).
echo hierarchical_term_tree();
function hierarchical_term_tree($category = 0)
{
$r = '';
$args = array(
'parent' => $category,
);
$next = get_terms('product_cat', $args);
if ($next) {
$r .= '<ul>';
foreach ($next as $cat) {
$r .= '<li><a href="' . get_term_link($cat->slug, $cat->taxonomy) . '" rel="nofollow noreferrer noopener" title="' . sprintf(__("View all products in %s"), $cat->name) . '" ' . '>' . $cat->name . ' (' . $cat->count . ')' . '</a>';
$r .= $cat->term_id !== 0 ? hierarchical_term_tree($cat->term_id) : null;
}
$r .= '</li>';
$r .= '</ul>';
}
return $r;
}
Simplified a little to take out the edit link etc. You can add those as required.
Method 3
You can use following code:
$args = array(
'hide_empty' => 0,
'echo' => 1,
'taxonomy' => 'category',
'hierarchical' =>1,
'show_count' => 1,
);
function add_class_wp_list_categories($wp_list_categories) {
$pattern = '/<li class="/is';
$replacement = '<li class="first ';
return preg_replace($pattern, $replacement, $wp_list_categories);
}
add_filter('wp_list_categories','add_class_wp_list_categories');
echo wp_list_categories( $args );
Method 4
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:
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 1,
'hide_empty' => '0'
);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0