WordPress免插件实现面包屑导航的示例代码


Posted in PHP onAugust 20, 2020

你如果在开发自己的wordpress主题,想加入面包屑导航,而又不想使用插件的话,下面的代码对你有帮助,这里提供了网上较为流行的两种代码,一是功能非常完善的,一是一个较为简洁的代码。

前面介绍了一种方法,你也可以尝试一下,在文末给大家详细介绍了自定义函数实现wordpress面包屑导航的代码,可以点击查看下。

功能非常完善代码

1、将下面的代码添加到主题的 functions.php

/**
 * WordPress 添加面包屑导航 
 * http://www.511yj.com/wordpress-add-breadcrumb.html
 */
function cmp_breadcrumbs() {
 $delimiter = '»'; // 分隔符
 $before = '<span class="current">'; // 在当前链接前插入
 $after = '</span>'; // 在当前链接后插入
 if ( !is_home() && !is_front_page() || is_paged() ) {
 echo '<div itemscope itemtype="http://schema.org/WebPage" id="crumbs">'.__( '当前位置:' , '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( __( 'Search Results for: %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( 'Not Found', '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>';
 }
}

前台调用

<?php if(function_exists('cmp_breadcrumbs')) cmp_breadcrumbs();?>

下面看下自定义函数实现wordpress面包屑导航的代码

面包屑导航 一是方便读者所在的位置,更重要的是对SEO非常友好,利于蜘蛛知道你网站的目录结构,所以给我们的wordpress主题添加面包屑导航是必须的。

1、在functioss.php添加以下代码

/**
 * WordPress 添加面包屑导航 
 * 面包屑导航,直接输出(echo)
 * Breadcrumb Trail
 * @param string $sep 导航对象分隔符,默认为' > '
 */
function bread_nav($sep = ' > '){
  echo '<div class="col-md-12 "><span class="glyphicon glyphicon-home text-primary"></span> 您当前的位置: <a href="'. home_url() .'" title="首页">首页</a>';
  if ( is_category() ){  //如果是栏目页面
    global $cat;    
    echo $sep . get_category_parents($cat, true, $sep) . '文章列表';
  }elseif ( is_page() ){  //如果是自定义页面
    echo $sep . get_the_title();
  }elseif ( is_single() ){  //如果是文章页面
    $categories = get_the_category();
    $cat = $categories[0];
    echo $sep . get_category_parents($cat->term_id, true, $sep) .'正文内容 '. get_the_title(); 
  }
  echo '</div>';
}

2、前台调用

<?php bread_nav();?>

总结

到此这篇关于WordPress免插件实现面包屑导航的示例代码的文章就介绍到这了,更多相关WordPress面包屑导航内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

PHP 相关文章推荐
PHP 生成的XML以FLASH获取为乱码终极解决
Aug 07 PHP
在PHP中PDO解决中文乱码问题的一些补充
Sep 06 PHP
PHPCMS的使用小结
Sep 20 PHP
phpmyadmin安装时提示:Warning: require_once(./libraries/common.inc.php)错误解决办法
Aug 18 PHP
关于PHP结束标签的使用细节探讨及联想
Mar 04 PHP
解析php安全性问题中的:Null 字符问题
Jun 21 PHP
php增删改查示例自己写的demo
Sep 04 PHP
php操作XML、读取数据和写入数据的实现代码
Aug 15 PHP
Php-Redis安装测试笔记
Mar 05 PHP
php表单提交实例讲解
Nov 12 PHP
PHP查询大量数据内存耗尽问题的解决方法
Oct 28 PHP
利用PHP获取访客IP、地区位置、浏览器及来源页面等信息
Jun 27 PHP
VSCode+PHPstudy配置PHP开发环境的步骤详解
Aug 20 #PHP
Laravel相关的一些故障解决
Aug 19 #PHP
聊聊 PHP 8 新特性 Attributes
Aug 19 #PHP
php提高脚本性能的4个技巧
Aug 18 #PHP
php判断IP地址是否在多个IP段内
Aug 18 #PHP
PHP 8新特性简介
Aug 18 #PHP
PHP大文件及断点续传下载实现代码
Aug 18 #PHP
You might like
PHP中strtotime函数使用方法分享
2012/01/10 PHP
php的闭包(Closure)匿名函数初探
2016/02/14 PHP
php中关于长度计算容易混淆的问题分析
2016/05/27 PHP
Ajax中的JSON格式与php传输过程全面解析
2017/11/14 PHP
PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】
2017/11/16 PHP
jscript之Open an Excel Spreadsheet
2007/06/13 Javascript
基于jQuery实现网页打印功能
2015/12/01 Javascript
jQuery 1.9.1源码分析系列(十五)之动画处理
2015/12/03 Javascript
【JS+CSS3】实现带预览图幻灯片效果的示例代码
2016/03/17 Javascript
用原生js统计文本行数的简单示例
2016/08/19 Javascript
JavaScript reduce和reduceRight详解
2016/10/24 Javascript
详解react-webpack2-热模块替换[HMR]
2017/08/03 Javascript
详解React Native 采用Fetch方式发送跨域POST请求
2017/11/15 Javascript
AngularJS基于MVC的复杂操作实例讲解
2017/12/31 Javascript
Vue.js最佳实践(五招助你成为vuejs大师)
2018/05/04 Javascript
解决vue-cli项目webpack打包后iconfont文件路径的问题
2018/09/01 Javascript
微信小程序自定义弹窗实现详解(可通用)
2019/07/04 Javascript
vue element-ui table组件动态生成表头和数据并修改单元格格式 父子组件通信
2019/08/15 Javascript
Vue3.0中的monorepo管理模式的实现
2019/10/14 Javascript
[02:33]DOTA2亚洲邀请赛趣味视频之吐真话筒
2018/03/31 DOTA
python3访问sina首页中文的处理方法
2014/02/24 Python
Python对文件和目录进行操作的方法(file对象/os/os.path/shutil 模块)
2017/05/08 Python
win10下python3.5.2和tensorflow安装环境搭建教程
2018/09/19 Python
python安装pil库方法及代码
2019/06/25 Python
python算法与数据结构之单链表的实现代码
2019/06/27 Python
django项目登录中使用图片验证码的实现方法
2019/08/15 Python
python使用celery实现异步任务执行的例子
2019/08/28 Python
pycharm 对代码做静态检查操作
2020/06/09 Python
Visual Studio Code搭建django项目的方法步骤
2020/09/17 Python
what is the difference between ext2 and ext3
2013/11/03 面试题
商场促销活动方案
2014/02/08 职场文书
租房协议书
2014/04/10 职场文书
硕士研究生就业推荐信
2014/05/18 职场文书
继承权公证书范本
2015/01/23 职场文书
工作态度不好检讨书
2015/05/06 职场文书
MySQL分区表实现按月份归类
2021/11/01 MySQL