Forums

[resolved] get_categories hierarchy=1 not working (8 posts)

  1. metepyers
    Member
    Posted 11 months ago #

    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>";
    }
  2. metepyers
    Member
    Posted 11 months ago #

    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???

  3. garylapointe
    Member
    Posted 11 months ago #

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

  4. MichaelH
    moderator
    Posted 11 months ago #

    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.

  5. garylapointe
    Member
    Posted 11 months ago #

    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

  6. MichaelH
    moderator
    Posted 11 months ago #

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

    wp_list_categories()

  7. garylapointe
    Member
    Posted 11 months ago #

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

  8. DavidPotter
    Member
    Posted 7 months ago #

    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

Reply

You must log in to post.

About this Topic