1

I have written this code for my breadcrumbs (i'll give only the part that I have problem with):

} elseif (is_page()) {
    if($post->post_parent){
        $anc = get_post_ancestors( $post->ID );

        foreach ( $anc as $ancestor ) {
            $output = '<li><a href="'.get_permalink($ancestor).'" title="'.get_the_title($ancestor).'">'.get_the_title($ancestor).'</a></li><li><i class="fa fa-angle-right"></i></li>';
            echo $output;
        }

So for a page I'm getting the array of ancestors and it works fine if I have single ancestor. When I have two of them or more then (by default as the codex states):

The direct parent is returned as the first value in the array. The highest level ancestor is returned as the last value in the array.

And that echoes out

HOME > PARENT 2 > PARENT 1 > PAGE TITLE

How can I sort the $anc array so I could reverse it?

0

OK Thought I need some special WP func to do it and as it turns out the simple answer is the best.

krsort($anc);
  • 1
    or just array_reverse – Milo May 13 '14 at 6:05
  • I reckon array_reverse is the correct answer. The keys (which reflect the ID’s) do not necessarily reflect the hierarchy. A page can become a parent after its children a created. – ACJ Jul 17 '18 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.