julia holland

Easy WordPress Breadcrumbs

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> &lt; ';
 
		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 ' &lt; ';
			}
                        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.