PHP code for WordPress breadcrumb navigation

We all know that breadcrumb navigation is very important for SEO, how to add this for our WordPress blogs then?

First, add the following code at the end of functions.php:



function get_breadcrumbs()
{
global $wp_query;
if ( !is_home() ){
// Start the UL
echo '

‘;
if ( is_category() )
{
$catTitle = single_cat_title( “”, false );
$cat = get_cat_ID( $catTitle );
echo ”

“;
}
elseif ( is_archive() && !is_category() )
{
echo ”

“;
}
elseif ( is_search() ) {
echo ”

“;
}
elseif ( is_404() )
{
echo ”

“;
}
elseif ( is_single() )
{
$category = get_the_category();
$category_id = get_cat_ID( $category[0]->cat_name );
echo ‘

“;
}
elseif ( is_page() )
{
$post = $wp_query->get_queried_object();
if ( $post->post_parent == 0 ){
echo ”

“;
} 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 ‘

‘;
} else {
echo ‘

‘;
}
}
}
}
// End the UL
echo ”

“;
}
}
?>

Second, add the following code at the proper place of single.php if you only need to show the breadcrumb navigation at post page only:


Last, add the css code at style.css:

ul.breadcrumbs {list-style: none; font-size:12px;}
ul.breadcrumbs li {float: left; margin-right:5px;}

Here is a sample: http://www.absorking.cn/
I only let breadcrumb navigation show at single post, so I add the following code at functions.php:

function get_breadcrumbs()
{
global $wp_query;
if ( is_single() )
{ echo '

“;
}
}
?>

I also change the style.css:

#breadcrumbs {
margin: 0px -10px 15px 0px;
font-size: 15px;
font-weight: bold;
height: 25px;
}