Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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";
}  
?>
share|improve this answer
    
genius! - hide_empty seems to require a numeric value to work, the rest is spot on - thanks! –  Q Studio Feb 8 '12 at 21:31
    
perhaps unrelated to my original question, but how could I determine the "level" of the category hierarchy each category is on - for example in a 3 level hierarchy, I'd need to get 1, 2 or 3? –  Q Studio Feb 8 '12 at 22:03
    
pastebin.com/GbZFnytw - depth output added as css class of ul tag –  Michael Feb 8 '12 at 22:24
    
amazing - is there any way to nest the ul / li's - so that they all wrap around - I'd like to try and get them into an accordion type open / close navigation? –  Q Studio Feb 9 '12 at 22:50
    
by nesting I meant <ul><li><ul><li></li></ul></li></ul> - I guess you figured that... –  Q Studio Feb 10 '12 at 9:00

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'
  );
share|improve this answer
    
    
Thanks - but this does not order them hierarchically - the codex states: -- When true, the results will include sub-categories that are empty, as long as those sub-categories have sub-categories that are not empty. The default is true. Valid values: –  Q Studio Feb 8 '12 at 21:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.