WordPress不可忽视的面包屑导航SEO优化技巧

什么是网站面包屑导航?面包屑导航意在告知用户所处的当前网页的位置,方便用户可以通过该导航快速返回上一级网页。
面包屑导航一般都在导航的下面,形式一般为 首页 > 一级目录名称 > 二级目录名称 > 目前位置,尽量要把面包屑导航的层次控制在四层以内,这样也有利与搜索引擎蜘蛛一层层往下爬,到了四层以下就很难爬到了。

面包屑导航的作用

  1. 让用户了解目前所处位置,以及当前页面在整个网站中的位置。
  2. 便于搜索引擎对于网站的收录,蜘蛛可以沿着面包屑导航爬下去,便于寻找链接,提高蜘蛛的爬行速度和效率;
  3. 便于用户了解网站的层次结构,进而浏览网站,提高用户体验;
  4. 优化面包屑导航每个层级的名称,尽量使用关键字,可以达到更好SEO优化的效果。

面包屑导航案例

//面包屑
function cmp_breadcrumbs() {
	$delimiter = '<i class="iconfont icon-arrow-right"></i>'; // 分隔符
	$before = '<span class="current">'; // 在当前链接前插入
	$after = '</span>'; // 在当前链接后插入
	if ( !is_home() && !is_front_page() || is_paged() ) {
		echo '<div class="crumb uk-overflow-container uk-text-nowrap">'.__( '<i class="iconfont icon-home-fill"></i>' , 'cmp' );
		global $post;
		$homeLink = home_url();
		echo ' <a itemprop="breadcrumb" href="'%20.%20$homeLink%20.%20'" rel="external nofollow" >' . __( '首页' , 'cmp' ) . '</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){
				$cat_code = get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' ');
				echo $cat_code = str_replace ('<a','<a itemprop="breadcrumb"', $cat_code );
			}
			echo $before . '' . single_cat_title('', false) . '' . $after;
		} elseif ( is_day() ) { // 天 存档
			echo '<a itemprop="breadcrumb" href="'%20.%20get_year_link(get_the_time('Y'))%20.%20'" rel="external nofollow"  rel="external nofollow" >' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
			echo '<a itemprop="breadcrumb"  href="'%20.%20get_month_link(get_the_time('Y'),get_the_time('m'))%20.%20'" rel="external nofollow" >' . get_the_time('F') . '</a> ' . $delimiter . ' ';
			echo $before . get_the_time('d') . $after;
		} elseif ( is_month() ) { // 月 存档
			echo '<a itemprop="breadcrumb" href="'%20.%20get_year_link(get_the_time('Y'))%20.%20'" rel="external nofollow"  rel="external nofollow" >' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
			echo $before . get_the_time('F') . $after;
		} elseif ( is_year() ) { // 年 存档
			echo $before . get_the_time('Y') . $after;
		} elseif ( is_single() && !is_attachment() ) { // 文章
			if ( get_post_type() != 'post' ) { // 自定义文章类型
				$post_type = get_post_type_object(get_post_type());
				$slug = $post_type->rewrite;
				echo '<a itemprop="breadcrumb" href="'%20.%20$homeLink%20.%20'/'%20.%20$slug['slug']%20.%20'/" rel="external nofollow" >' . $post_type->labels->singular_name . '</a> ' . $delimiter . ' ';
				echo $before . get_the_title() . $after;
			} else { // 文章 post
				$cat = get_the_category(); $cat = $cat[0];
				$cat_code = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
				echo $cat_code = str_replace ('<a','<a itemprop="breadcrumb"', $cat_code );
				echo $before . get_the_title() . $after;
			}
		} elseif ( !is_single() && !is_page() && get_post_type() != 'post' ) {
			$post_type = get_post_type_object(get_post_type());
			echo $before . $post_type->labels->singular_name . $after;
		} elseif ( is_attachment() ) { // 附件
			$parent = get_post($post->post_parent);
			$cat = get_the_category($parent->ID); $cat = $cat[0];
			echo '<a itemprop="breadcrumb" href="'%20.%20get_permalink($parent)%20.%20'" rel="external nofollow" >' . $parent->post_title . '</a> ' . $delimiter . ' ';
			echo $before . get_the_title() . $after;
		} elseif ( is_page() && !$post->post_parent ) { // 页面
			echo $before . get_the_title() . $after;
		} elseif ( is_page() && $post->post_parent ) { // 父级页面
			$parent_id  = $post->post_parent;
			$breadcrumbs = array();
			while ($parent_id) {
				$page = get_page($parent_id);
				$breadcrumbs[] = '<a itemprop="breadcrumb" href="'%20.%20get_permalink($page->ID)%20.%20'" rel="external nofollow" >' . get_the_title($page->ID) . '</a>';
				$parent_id  = $page->post_parent;
			}
			$breadcrumbs = array_reverse($breadcrumbs);
			foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
			echo $before . get_the_title() . $after;
		} elseif ( is_search() ) { // 搜索结果
			echo $before ;
			printf( __( '搜索「%s」的结果如下:', 'cmp' ),  get_search_query() );
			echo  $after;
		} elseif ( is_tag() ) { //标签 存档
			echo $before ;
			printf( __( 'Tag Archives: %s', 'cmp' ), single_tag_title( '', false ) );
			echo  $after;
		} elseif ( is_author() ) { // 作者存档
			global $author;
			$userdata = get_userdata($author);
			echo $before ;
			printf( __( 'Author Archives: %s', 'cmp' ),  $userdata->display_name );
			echo  $after;
		} elseif ( is_404() ) { // 404 页面
			echo $before;
			_e( '没有找到', 'cmp' );
			echo  $after;
		}
		if ( get_query_var('paged') ) { // 分页
			if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
				echo sprintf( __( '( Page %s )', 'cmp' ), get_query_var('paged') );
		}
		echo '</div>';
	}
}

使用方法

将上面WordPress代码复制粘贴到自己主题的functions.php里,然后再需要显示面包屑导航的页面添加下面代码即可。

关于WordPress不可忽视的面包屑导航SEO优化技巧的相关内容;如有侵权,请联系老文删除。

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
老文的头像老文
上一篇 2021年9月2日 上午1:23
下一篇 2021年9月4日 上午12:24

相关推荐

  • 使用测速脚本对 Linux 服务器进行测速

    最近又搞了个美国的服务器来学习,但是发现在国内直连速度特别慢,就想知道一下他的速度到底怎么样?在厘米大神那找了个综合测速脚本进行测速,并记录如下! 使用 root 账号登陆服务器,输入以下指令: wget -qO- bench.sh | bash 通过这个测速脚本,缙哥哥发现服务器方面,Pump Cloud 是真的不错,阿里云次之,最近买来学习Linux的那…

    2021年8月12日
    687
  • DZ论坛UCenter后台通信失败彻底解决方法(亲测有效)

    前面在论坛分享过discuz论坛更换目录后出现头像无法显示/ucenter无法进入,只是简单地讲了在更换子目录之后怎么重新设置参数,使得头像正常显示。 但是今天在迁移一个新的站点的时候发现ucenter彻底无法进入,直接是空白显示,如图。 另外全站会员都没法加载头像,图片显示失败。 这时候我考虑到是UCenter的链接是有问题的,但是我的uc_server文…

    2024年7月31日 • 建站知识
    362
  • WordPress怎么自动发布文章

    WordPress具有定期发布文章的功能。在发布界面中,有一个按钮用于立即发布和编辑文章。单击此处可编辑发布文章的时间。 因此,我们可以设置在特定时间发布文章的时间: 为什么我们需要定期发表文章?定期发表文章有哪些优点和缺点?为什么我们需要使用WordPress的定时发帖功能?对于搜索引擎和用户体验来说,在固定时间发布文章是一种非常好的体验。 因为如果我们网…

    2023年1月4日
    250
  • wordpress批量删除所有用户的数据库命令

    一、wordpress如何删除所有用户,只保留管理员 据说可以用插件。但我还是感觉sql命令更直接。 提醒:数据库任何操作之前,都要确保好已经做了备份! 1:删除没有文章的用户 DELETE FROM wp_users WHERE ID NOT IN (SELECT post_author FROM wp_posts); 2:删除不存在用户的元数据 DELE…

    2021年8月12日
    750
  • WordPress在前台显示登录用户的注册时间

    在使用WordPress建站的时候,有的用户希望能在前台显示用户注册时间,但是WordPress是没有这个选项的,那么该如何实现这个功能呢? 非常简单,虽然这个功能并不常见,也没多大的作用。这个功能基本上是用在有用户中心站点。 首先将下面代码复制到主题的functions.php文章中。 //WordPress在前台显示登录用户的注册时间 function …

    2021年11月2日
    513