• Resolved metepyers

    (@metepyers)


    I want to get the categories listed in hierarchical order. But this code is returning my categories in alphabetical order, and ignoring the hierarchy. Any help?

    $categories = get_categories("hierarchical=1");
    foreach ($categories as $cat) {
    	echo "<p>".$cat->cat_name."</p>";
    }

    I’m pretty confident that this code is correct. I’m tempted to call this a bug. I’ve also tried:

    $categories = get_categories("hierarchical=true");
    foreach ($categories as $cat) {
    	echo "<p>".$cat->cat_name."</p>";
    }

    and:

    $parameters = array('type' => 'post',
    	'orderby' => 'name',
    	'order' => 'ASC',
    	'hide_empty' => false,
    	'include_last_update_time' => false,
    	'hierarchical' => 1,
    	'pad_counts' => false);
    $categories = get_categories($parameters);
    foreach ($categories as $cat) {
    	echo "<p>".$cat->cat_name."</p>";
    }

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter metepyers

    (@metepyers)

    I really don’t understand this. This code does put the categories in hierarchical order:

    $categories = wp_list_categories("hierarchical=1");

    But this doesn’t:

    $categories = get_categories("hierarchical=1");

    But, I checked, and wp_list_categories is using get_categories itself – so why doesn’t it work for me.

    Any help… anyone???

    I’ve Googled all around on this too. Seems like a pretty simple thing. Seems like the solution would already be posted somewhere ??

    wp_list_categories uses the walk_category_tree to set the hierarchy after it call get_categories to retrieve all the categories. Look at wp_list_categories function in wp-includes/category-template.php to see that.

    Don’t forget the echo=0 ability of wp_list_categories to return then list of categories instead of display them.

    MichaelH,

    So are you saying we can somehow use wp_list_categories (instead of get_categories) and have it sort the list for us properly? Similar to @metepyers first example?

    Can you help with the passing of the parameters? I’m note sure if I need echo=0, echo=’0′, echo=”0″, “echo=0”, $echo=0, etc… (And obviously we need it hierarchical). I haven’t played with parameters much and just can’t get this…

    Thank you,
    Gary

    Sorry, I should have pointed to the documentation on that:

    wp_list_categories()

    Thanks! That’ll keep me busy reading for a bit!

    So how do I get a collection of category objects that are sorted so that child categories always come after their parent? Is this something I have to write myself?

    Thanks,
    David

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘get_categories hierarchy=1 not working’ is closed to new replies.