欢迎来 起落博客 加入收藏 设为主页

wordpress教程:wordpress面包屑导航



完善博客的过程中,翻看文章分类列表页面没有提示当下文章是哪个类别的,于是打算添加面包屑导航,也利于seo

方法:打开主题文件夹中的functions.php,加入以下代码:

function dimox_breadcrumbs() {$delimiter = ‘»’;
$name = ‘Home’; //text for the ‘Home’ link
$currentBefore = ‘<span>’;
$currentAfter = ‘</span>’;

if ( !is_home() && !is_front_page() || is_paged() ) {

echo ‘<div id=”crumbs”>’;

global $post;
$home = get_bloginfo(‘url’);
echo ‘<a href=”‘ . $home . ‘”>’ . $name . ‘</a> ‘ . $delimiter . ‘ ‘;

if ( is_category() ) {
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ‘ ‘ . $delimiter . ‘ ‘));
echo $currentBefore . ‘Archive by category &#39;’;
single_cat_title();
echo ‘&#39;’ . $currentAfter;

} elseif ( is_day() ) {
echo ‘<a href=”‘ . get_year_link(get_the_time(‘Y’)) . ‘”>’ . get_the_time(‘Y’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo ‘<a href=”‘ . get_month_link(get_the_time(‘Y’),get_the_time(‘m’)) . ‘”>’ . get_the_time(‘F’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $currentBefore . get_the_time(‘d’) . $currentAfter;

} elseif ( is_month() ) {
echo ‘<a href=”‘ . get_year_link(get_the_time(‘Y’)) . ‘”>’ . get_the_time(‘Y’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $currentBefore . get_the_time(‘F’) . $currentAfter;

} elseif ( is_year() ) {
echo $currentBefore . get_the_time(‘Y’) . $currentAfter;

} elseif ( is_single() ) {
$cat = get_the_category(); $cat = $cat[0];
echo get_category_parents($cat, TRUE, ‘ ‘ . $delimiter . ‘ ‘);
echo $currentBefore;
the_title();
echo $currentAfter;

} elseif ( is_page() && !$post->post_parent ) {
echo $currentBefore;
the_title();
echo $currentAfter;

} elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = ‘<a href=”‘ . get_permalink($page->ID) . ‘”>’ . get_the_title($page->ID) . ‘</a>’;
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb . ‘ ‘ . $delimiter . ‘ ‘;
echo $currentBefore;
the_title();
echo $currentAfter;

} elseif ( is_search() ) {
echo $currentBefore . ‘Search results for &#39;’ . get_search_query() . ‘&#39;’ . $currentAfter;

} elseif ( is_tag() ) {
echo $currentBefore . ‘Posts tagged &#39;’;
single_tag_title();
echo ‘&#39;’ . $currentAfter;

} elseif ( is_author() ) {
global $author;
$userdata = get_userdata($author);
echo $currentBefore . ‘Articles posted by ‘ . $userdata->display_name . $currentAfter;

} elseif ( is_404() ) {
echo $currentBefore . ‘Error 404’ . $currentAfter;
}

if ( get_query_var(‘paged’) ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ‘ (‘;
echo __(‘Page’) . ‘ ‘ . get_query_var(‘paged’);
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ‘)’;
}

echo ‘</div>’;

}
}

然后在适当的地方(本博客是加在header页面的)加上:

<?php if (function_exists(‘dimox_breadcrumbs’)) dimox_breadcrumbs(); ?>

这样直接添加的面包屑导航样式可能不是很完美,适当修改下css样式就行了。


原创文章,转载请注明:起落SEO博客
本文链接地址: http://76tui.net/archives/571.html

发表评论

Your email is never published nor shared. Required fields are marked *

*

*

=388*

输入您的评论:


评论