WordPress教程

WordPress 添加面包屑导航的二种方法

阿里云

面包屑导航,简单的说它就是提供给用户回溯到网站首页或入口页面的一条快速路径。今天创客分享一下 WordPress 添加面包屑导航的三种方法,希望对大家有所帮助,原文参考自园子博客。

面包屑定义:

面包屑通常出现在页面顶部,一般会位于标题或页头的下方。它提供给用户返回之前任何一个页面的链接(这些链接也是能到达当前页面的路径),在层级架构中通常是这个页面的父级页面。也可以这样理解,面包屑提供给用户回溯到网站首页或入口页面的一条快速路径,它们绝大部分看起来就像这样:首页→分类页→次级分类页。如下图所示:
breadcrumbs

也想出现在这里?联系我们
创客主机

面包屑好处:

1.可以提供多路径的交互方式,方便用户跳转到其它页面。在页面及分类多的网站中尤其有用。

2.面包屑导航信息结构对于网站的 SEO 也有着大的好处,它可以更多的强调网站关键字,扩大关键字的范围,从而达到更好的优化目的。

3.它从一个侧面展示了该信息集合的信息结构和集合方式,可以让用户在最快的时间之内找到需要的东西。

方案一:

把以下代码直接添加到你想出现面包屑导航的位置,比如 header.php 里面,也可以放在 single.php 页面的导航标题上面,你有可能需要添加的页面可能有:archive.php、archives.php、links.php、page.php。

  1. <div class="mbx-dh">
  2. 当前位置:<a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a> &raquo;
  3. <?php
  4. if( is_single() ){
  5. $categorys = get_the_category();
  6. $category = $categorys[0];
  7. echo( get_category_parents($category->term_id,true,' &raquo; ') );
  8. the_title();
  9. } elseif ( is_page() ){
  10. the_title();
  11. } elseif ( is_category() ){
  12. single_cat_title();
  13. } elseif ( is_tag() ){
  14. single_tag_title();
  15. } elseif ( is_day() ){
  16. the_time('Y年Fj日');
  17. } elseif ( is_month() ){
  18. the_time('Y年F');
  19. } elseif ( is_year() ){
  20. the_time('Y年');
  21. } elseif ( is_search() ){
  22. echo $s.' 的搜索结果';
  23. }
  24. ?>
  25. </div>

方案二:

首先把以下代码添加到主题的 functions.php 文件中

  1. function dimox_breadcrumbs() {
  2.  
  3.   $delimiter = '&raquo;';
  4.   $name = 'Home'; //text for the 'Home' link
  5.   $currentBefore = '<span>';
  6.   $currentAfter = '</span>';
  7.  
  8.   if ( !is_home() && !is_front_page() || is_paged() ) {
  9.  
  10.     echo '<div id="crumbs">';
  11.  
  12.     global $post;
  13.     $home = get_bloginfo('url');
  14.     echo '' . $name . ' ' . $delimiter . ' ';
  15.  
  16.     if ( is_category() ) {
  17.       global $wp_query;
  18.       $cat_obj = $wp_query->get_queried_object();
  19.       $thisCat = $cat_obj->term_id;
  20.       $thisCat = get_category($thisCat);
  21.       $parentCat = get_category($thisCat->parent);
  22.       if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
  23.       echo $currentBefore . 'Archive by category &#39;';
  24.       single_cat_title();
  25.       echo '&#39;' . $currentAfter;
  26.  
  27.     } elseif ( is_day() ) {
  28.       echo '' . get_the_time('Y') . ' ' . $delimiter . ' ';
  29.       echo '' . get_the_time('F') . ' ' . $delimiter . ' ';
  30.       echo $currentBefore . get_the_time('d') . $currentAfter;
  31.  
  32.     } elseif ( is_month() ) {
  33.       echo '' . get_the_time('Y') . ' ' . $delimiter . ' ';
  34.       echo $currentBefore . get_the_time('F') . $currentAfter;
  35.  
  36.     } elseif ( is_year() ) {
  37.       echo $currentBefore . get_the_time('Y') . $currentAfter;
  38.  
  39.     } elseif ( is_single() ) {
  40.       $cat = get_the_category(); $cat = $cat[0];
  41.       echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
  42.       echo $currentBefore;
  43.       the_title();
  44.       echo $currentAfter;
  45.  
  46.     } elseif ( is_page() && !$post->post_parent ) {
  47.       echo $currentBefore;
  48.       the_title();
  49.       echo $currentAfter;
  50.  
  51.     } elseif ( is_page() && $post->post_parent ) {
  52.       $parent_id  = $post->post_parent;
  53.       $breadcrumbs = array();
  54.       while ($parent_id) {
  55.         $page = get_page($parent_id);
  56.         $breadcrumbs[] = '' . get_the_title($page->ID) . '';
  57.         $parent_id  = $page->post_parent;
  58.       }
  59.       $breadcrumbs = array_reverse($breadcrumbs);
  60.       foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
  61.       echo $currentBefore;
  62.       the_title();
  63.       echo $currentAfter;
  64.  
  65.     } elseif ( is_search() ) {
  66.       echo $currentBefore . 'Search results for &#39;' . get_search_query() . '&#39;' . $currentAfter;
  67.  
  68.     } elseif ( is_tag() ) {
  69.       echo $currentBefore . 'Posts tagged &#39;';
  70.       single_tag_title();
  71.       echo '&#39;' . $currentAfter;
  72.  
  73.     } elseif ( is_author() ) {
  74.        global $author;
  75.       $userdata = get_userdata($author);
  76.       echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;
  77.  
  78.     } elseif ( is_404() ) {
  79.       echo $currentBefore . 'Error 404' . $currentAfter;
  80.     }
  81.  
  82.     if ( get_query_var('paged') ) {
  83.       if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  84.       echo __('Page') . ' ' . get_query_var('paged');
  85.       if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  86.     }
  87.  
  88.     echo '</div>';
  89.  
  90.   }
  91. }

最后在适当的地方(如方法一中提到的几个文件)添加以下代码调用:

  1. <div class="mbx-dh">
  2. <?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs(); ?> 
  3. </div>

如果想要美化下显示方式,直接通过添加 css 即可:

  1. .mbx-dh {padding: 5px 10px;}

WordPress 添加面包屑导航的二种方法

已有 877 人购买
查看演示升级 VIP立刻购买

收藏
(4)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!