get_categories hierarchical order like wp_list_categories – with name, slug & link to edit cat

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.

4 s
4

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 ) . '" 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" title="Edit Category">Edit</a>'; 
    hierarchical_category_tree( $cat->term_id );
    endforeach;    
  endif;

  echo '</li></ul>'; echo "\n";
}  
?>

Leave a Comment