WordPress breadcrumb navigation PHP code

Here’s some really simple PHP code to generate breadcrumb navigation in WordPress. It only supports navigation down two levels – i.e. you have a page that defines a section of a site, and then child pages within the section. It doesn’t support grandchild or deeper navigation.

The code is laid out in a really simple way, so you can see exactly how it works. Just place the following code in your QWordPress template functions.php file:

function get_breadcrumb() {
global $wp_query;
echo(‘<a href=”‘);
bloginfo(‘siteurl’);
echo(‘”>Home</a>’);
if(!is_home()) {
echo(‘ &raquo; ‘);
if (!empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->post_parent;
echo(‘<a href=”‘);
echo(get_permalink($parent));
echo(‘”> ‘);
echo(get_the_title($parent));
echo(‘</a> &raquo; ‘);
}
}
echo(‘<a href=”‘);
get_permalink();
echo(‘”>’);
the_title();
echo(‘</a>’);
}

To generate the breadcrumbs, just use

get_breadcrumb();

within any of the php files for your template.

Leave a Reply