Posted On January 5, 2012

WordPress breadcrumb navigation PHP code

admin 0 comments

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.

Related Post

WordPress PHP code for sidebar with dynamic content determined by current page

Here is some really simple PHP code that I wrote for a client's WordPress site.…

Is content marketing really all about product placement?

Trinity Mirror has launched a website funded entirely by native advertising. I'm not sure this…

PHP code for WordPress side menu

I've been developing a website for a client who wanted a dynamic side menu. The…