WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sort the_category not alphabetical but by parents and children (5 posts)

  1. addictable
    Member
    Posted 1 year ago #

    If you take a look at http://addictable.de/, you can see that the categories that an article are published in are displayed below the title. I'd like the output to be sorted by parents and children.

    For example: "Seelen", "Warm Bodies" and "Die Tribute von Panem" are all children of "Filme". So the correct output should be "Filme | Seelen", "Filme | Warm Bodies" and "Filme | Die Tribute von Panem".

    On the first two articles, the output is alright, because I guess that the categories are sorted alphabetically. But as "Die Tribute von Panem" starts with a D and "Filme" starts with a F on the third post, the output is not correct.

    Could you tell me where to change/add the code so that "the_category" is sorted hierarchically and not alphabetically?

    I've been looking for a resolution for a while now and I can't find one anywhere. Thanks! :)

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    If your theme is using one of these functions to display the categories:
    http://codex.wordpress.org/Function_Reference/the_category
    http://codex.wordpress.org/Function_Reference/get_the_category
    http://codex.wordpress.org/Function_Reference/get_the_category_list

    you can replace that code with this to show the hierarchical order:

    <?php
    $categories = wp_get_post_categories( $post->ID, array('fields' => 'ids'));
    if($categories) {
     	$cat_ids = implode(',' , $categories);
     	$cats = wp_list_categories('title_li=&style=none&echo=0&include='.$cat_ids);
     	echo str_replace('<br />', ' | ', $cats);
    }
    ?>
  3. addictable
    Member
    Posted 1 year ago #

    Thank you, that worked perfectly fine! :)

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you've got it resolved.

  5. snickn
    Member
    Posted 1 year ago #

    This is awesome. Almost perfectly what I'm looking for, I'm wondering if you can advise how I can make it list as parent(child) instead of parent|child| (there will always only be one child)

    Not sure if I can cross post, but I posted another post on why my child categories 404

    Maybe you have insight!

    thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic