I´m trying to create a function that creates a breadcrumb trail of Page slugs outside of The Loop. These breadcrumbs show the path from the homepage to the current (sub)page.
For example, on the page "example.com/attractions/parcs/parc-name" this breadcrumb would be shown: Home > Attractions > Parcs > Parc Name
I´ve done a lot of research and found various code snippets that can perform part of the function, but my PHP skills are not good enough to create the whole function myself.
This is what I have:
- Get the post slug in The Loop:
$slug = basename(get_permalink());
(src) - Get the post slug outside of The Loop:
global $post; echo $post->post_name;
(src: see above) - Get posts´parent slug
<?php global $post; if($post->post_parent) { $post_data = get_post($post->post_parent); echo $post_data->post_name; } ?>
(src) Get breadcrumb trail of pages using page titles (this is what I´m using now):
if ( is_page() ) { $post = $wp_query->get_queried_object(); if ( $post->post_parent == 0 ) { echo "<li> » ".the_title('','', FALSE)."</li>"; } else { $title = the_title('','', FALSE); $ancestors = array_reverse( get_post_ancestors( $post->ID ) ); array_push($ancestors, $post->ID); foreach ( $ancestors as $ancestor ) { if( $ancestor != end($ancestors) ) { echo '<li> » <a href="'. get_permalink($ancestor) .'">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</a></li>'; } else { echo '<li> » '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</li>'; } } } }
All ideas, suggestions and solutions are very much appreciated :)