L
L
ltrps2021-07-15 10:30:34
WordPress
ltrps, 2021-07-15 10:30:34

How to sort the_category output by hierarchy?

A photo is attached to the post, the post is in the "Color" category and in the "Red" subcategory. On the attachment photo page, I try to display links to the categories to which the post belongs in this way:

<?php the_category( ' / ', '', $post->post_parent ); ?>


It turns out a construction of the form: Color / Red

Everything is fine, but when a subcategory begins with a letter higher in the alphabet (Black), then the construction is reversed, it turns out: Black / Color

How to sort by hierarchy, not name?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bingumd, 2021-07-15
@bingumd

<?php the_category( ' / ', 'multiple', $post->post_parent ); ?>

L
ltrps, 2021-07-15
@ltrps

I tried it, it also sorts by name, plus duplication of the parent category is added, it turns out like this:
Color / Color / Red
Color / Black / Color

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question