Posted by on June 3, 2014

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:


';
// Add the Home link
echo '

  • '. get_bloginfo('name') .'
  • ';
    if ( is_category() )
    {
    $catTitle = single_cat_title( "", false );
    $cat = get_cat_ID( $catTitle );
    echo "

  • » ". get_category_parents( $cat, TRUE, " » " ) ."
  • ";
    }
    elseif ( is_archive() && !is_category() )
    {
    echo "

  • » Archives
  • ";
    }
    elseif ( is_search() ) {
    echo "

  • » Search Results
  • ";
    }
    elseif ( is_404() )
    {
    echo "

  • » 404 Not Found
  • ";
    }
    elseif ( is_single() )
    {
    $category = get_the_category();
    $category_id = get_cat_ID( $category[0]->cat_name );
    echo '

  • » '. get_category_parents( $category_id, TRUE, " » " );
    echo the_title('','', FALSE) ."
  • ";
    }
    elseif ( is_page() )
    {
    $post = $wp_query->get_queried_object();
    if ( $post->post_parent == 0 ){
    echo "

  • » ".the_title('','', FALSE)."
  • ";
    } 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 '

  • » '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'
  • ';
    } else {
    echo '

  • » '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'
  • ';
    }
    }
    }
    }
    // 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:
    ';
    echo ''. get_bloginfo('name') .'';
    $category = get_the_category();
    $category_id = get_cat_ID( $category[0]->cat_name );
    echo ' » '. get_category_parents( $category_id, TRUE, " » " );
    echo the_title('','', FALSE);
    echo "

    ";
    }
    }
    ?>

    I also change the style.css:

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

    Posted in: WordPress

    Comments

    Be the first to comment.

    Leave a Reply