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

本文共7175个字,预计阅读时间需要18分钟 发布时间: 2021-12-19 共44人阅读

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

面包屑导航的作用

  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=\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\' . $homeLink . \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\" 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=\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\' . get_year_link(get_the_time(\\\\\\\\\\\\\\\'Y\\\\\\\\\\\\\\\')) . \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\" rel=\\\\\\\\\\\\\\\"external nofollow\\\\\\\\\\\\\\\"  rel=\\\\\\\\\\\\\\\"external nofollow\\\\\\\\\\\\\\\" >\\\\\\\\\\\\\\\' . get_the_time(\\\\\\\\\\\\\\\'Y\\\\\\\\\\\\\\\') . \\\\\\\\\\\\\\\'</a> \\\\\\\\\\\\\\\' . $delimiter . \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\';
			echo \\\\\\\\\\\\\\\'<a itemprop=\\\\\\\\\\\\\\\"breadcrumb\\\\\\\\\\\\\\\"  href=\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\' . get_month_link(get_the_time(\\\\\\\\\\\\\\\'Y\\\\\\\\\\\\\\\'),get_the_time(\\\\\\\\\\\\\\\'m\\\\\\\\\\\\\\\')) . \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\" rel=\\\\\\\\\\\\\\\"external nofollow\\\\\\\\\\\\\\\" >\\\\\\\\\\\\\\\' . get_the_time(\\\\\\\\\\\\\\\'F\\\\\\\\\\\\\\\') . \\\\\\\\\\\\\\\'</a> \\\\\\\\\\\\\\\' . $delimiter . \\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\';
			echo $before . get_the_time(\\\\\\\\\\\\\\\'d\\\\\\\\\\\\\\\') . $after;
		} elseif ( is_month() ) { // 月 存档
			echo \\\\\\\\\\\\\\\'<a itemprop=\\\\\\\\\\\\\\\"breadcrumb\\\\\\\\\\\\\\\" href=\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\' . get_year_link(get_the_time(\\\\\\\\\\\\\\\'Y\\\\\\\\\\\\\\\')) . \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\" 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=\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\' . $homeLink . \\\\\\\\\\\\\\\'/\\\\\\\\\\\\\\\' . $slug[\\\\\\\\\\\\\\\'slug\\\\\\\\\\\\\\\'] . \\\\\\\\\\\\\\\'/\\\\\\\\\\\\\\\" 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=\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\' . get_permalink($parent) . \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\" 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=\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\' . get_permalink($page->ID) . \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\" 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里,然后再需要显示面包屑导航的页面添加下面代码即可。

<?php cmp_breadcrumbs();?>

 

本站声明:
1. 本站所有资源来源于网络,分享目的仅供大家学习和交流! 2. 不得使用于非法商业用途,不得违反国家法律。否则后果自负! 3. 本站提供的资源,都不包含技术服务请大家谅解! 4. 如有链接无法下载、失效或广告,请联系站长处理!

再次声明:如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。


HI模板网 » WordPress不可忽视的面包屑导航SEO优化技巧

常见问题FAQ

本站下载资源解压密码是什么?
本站所有资源解压密码均为www.hmoban.com、www.huziy.com或者www.92luntan.com
免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
开通VIP 享更多特权,为了您的账户安全HI模板网建议您使用QQ登录!