Display categories in hierarchical order on posts

  • AIRCOM

    #6766

    Hello!

    I need a little help : WordPress displays categories with the post, by default sorts them in alphabetical order. However it would be more logical for my blog to have them arranged in a hierarchical way.

    For exemple, under my title post I would like to have : Movies -> hitchcock -> Lifeboat, where “Hitchcock” is a child of “Movies” and “Lifeboat” in a child of “Hitchcock”.

    Instead, I have Hichcock, Lifeboat, Movie. (Since they are sort in alphabetical order.)

    I found this piece of code which should do want I want :

    <?php
    $cat_Id = get_the_category($post->ID);
    $categories = get_category_parents( $cat_Id[0], TRUE, ' > ' );
    $title = get_the_title();
    printf( '%1$s <span>in %2$s</span>', get_the_date(), $categories . $title );
    ?>

    My problem is that I have no idea where to put it… I tried to put it in the loop.php, single.php, archive.php… but nothing changed… :/

    Is anyone have an idea where to put this code? Or maybe this code is wrong… I’m not really good at PHP!

    Thanks fo any help!

    Admin

    Syahir Hakim

    #35525

    What you’re describing sounds more like a breadcrumb. You might want to try the Breadcrumb NavXT plugin.

    ChainKiller

    #35526

    You can achieve that by editing the loop.php and loop-single.php files ( you can do it under WordPress dashboard appearance > editor or from cPanel)

    Search for

    <?php if ( ( $graphene_settings['hide_post_cat'] != true ) ) : ?>
    <span class="printonly"><?php _e( 'Categories:', 'graphene' ); ?> </span>
    <p class="meta_categories"><?php the_category( ", " ); ?></p>
    <?php endif; ?>

    there replace `

    <?php the_category( “, ” ); ?> with

    <?php the_category( ” > “,”multiple” ); ?>` and when you add the category you choose only the last child category in your case only Lifeboat

    and you will get Movies > hitchcock > Lifeboat

    Check my site to see how it looks anime13.tk

    <?php the_category( " > ","multiple" ); ?>

    the > is the separator beetween the categories you can put any separator you want

    I recommend you to create a child theme and do the changes there.

    AIRCOM

    #35527

    Wow! That’s perfect! It is exactly what I wanted. Much easy than the way I was going to do.

    Thank you very much.

    Syahir, I’m also going to put a breadcrumb, I heard it’s good for SEO…

    Mod

    Kenneth John Odle

    #35528

    Keep in mind that if you edit theme files, you will lose your changes the next time the theme updates.

Viewing 5 posts - 1 through 5 (of 5 total)

  • You must be logged in to reply to this topic.