Display categories in hierarchical order on posts
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 :
$cat_Id = get_the_category($post->ID);
$categories = get_category_parents( $cat_Id, 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
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)
<?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( “, ” ); ?>
<?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.
You must be logged in to reply to this topic.