Recently a client project required a breadcrumb navigation for all interior pages. After a bit of research and discovery of the useful WP function get_post_ancestors I created the following:
Add to functions.php
function my_breadcrumb() { if (!is_home()) { global $post; echo '<a href="'; echo bloginfo('url'); echo '">Home</a> < '; if(is_page() && $post->post_parent) { $anc = get_post_ancestors( $post->ID ); $anc = array_reverse($anc); foreach($anc as $ancestor) { echo '<a href="' . get_permalink($ancestor) . '">'; echo get_the_title($ancestor); echo '</a>'; echo ' < '; } echo the_title(); } elseif (is_page() && !$post->post_parent) { echo the_title(); } } }
Add to page template
<?php my_breadcrumb(); ?>
Creates the simple breadcrumb:
Home < Page I’m On
This was written for use only on WP Pages. If you want to use for your Posts, Tags (and others) you will need to include a case for is_single(), is_category(), is_tag(), etc.
This is a great breadcrumb script…thanks for making it available.
I was wondering if it is possible to modify the script so that instead of echoing the “page title” for the breadcrumb, is it possible to use the value specified in a custom field instead?
Thus, would it be possible to specify a custom field on every page, with a key of say “breadcrumb_title”. Could this script be modified to substitute in “breadcrumb_title” for “the_title”?