Doing same thing in more concise way:
function breadcrump_page( $post ) {
$format = '<a href="%s" title="%s">%s</a> >';
$anc = array_map( 'get_post', array_reverse( (array) get_post_ancestors( $post ) ) );
$links = array_map( 'get_permalink', $anc );
foreach ( $anc as $i => $apost ) {
$title = apply_filters( 'the_title', $apost->post_title );
printf( $format, $links[$i], esc_attr($title), esc_html($title) );
}
echo apply_filters( 'the_title', $post->post_title );
}
global $post;
if ( $post->post_parent ) breadcrump_page( $post );
WordPress has a core function get_post_ancestors
that retrieve all the parent ids for hierarchical post.
I called it, reverse the order becuse they are returned form direct parent to highest ancestor, and finally get all post objects and all permalinks using a couple of array_map
array_map
.
Doing that, is just matter of glue all the pieces and output them.