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 表单提交给自己
Jul 24 PHP
简单的PHP多图上传小程序代码
Jul 17 PHP
用PHP实现的四则运算表达式计算实现代码
Aug 02 PHP
PHP自动生成后台导航网址的最佳方法
Aug 27 PHP
php获取远程图片体积大小的实例
Nov 12 PHP
YII路径的用法总结
Jul 09 PHP
利用PHP将部分内容用星号替换
Apr 21 PHP
修改PHP脚本使WordPress拦截垃圾评论的方法示例
Dec 10 PHP
PHP模板引擎Smarty内建函数foreach,foreachelse用法分析
Apr 11 PHP
PHP实现的杨辉三角求解算法分析
Mar 11 PHP
微信公众平台开发教程③ PHP实现微信公众号支付功能图文详解
Apr 10 PHP
php文件后缀不强制为.php的实操方法
Sep 18 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图片等比缩放类SimpleImage使用方法和使用实例分享
2014/04/10 PHP
PHP连接access数据库
2015/03/27 PHP
php使用array_search函数实现数组查找的方法
2015/06/12 PHP
thinkphp5框架API token身份验证功能示例
2019/05/21 PHP
IE下window.onresize 多次调用与死循环bug处理方法介绍
2013/11/12 Javascript
jQuery实现查找最近父节点的方法
2016/06/23 Javascript
JS html时钟制作代码分享
2017/03/03 Javascript
js精确的加减乘除实例
2017/11/14 Javascript
Vue.use源码学习小结
2018/06/20 Javascript
web页面和微信小程序页面实现瀑布流效果
2018/09/26 Javascript
微信小程序登录按钮遮罩浮层效果的实现方法
2018/12/16 Javascript
如何写好一个vue组件,老夫的一年经验全在这了(推荐)
2019/05/18 Javascript
小程序使用watch监听数据变化的方法详解
2019/09/20 Javascript
javascript实现点击产生随机图形
2021/01/25 Javascript
[01:58]DOTA2上海特级锦标赛现场采访:RTZ这个ID到底好不好
2016/03/25 DOTA
[57:22]完美世界DOTA2联赛PWL S2 FTD vs PXG 第二场 11.27
2020/12/01 DOTA
python多重继承新算法C3介绍
2014/09/28 Python
用python读写excel的方法
2014/11/18 Python
python简单实现基于SSL的IRC bot实例
2015/06/15 Python
Python3 操作符重载方法示例
2017/11/23 Python
Django REST为文件属性输出完整URL的方法
2017/12/18 Python
Python中装饰器高级用法详解
2017/12/25 Python
python监控键盘输入实例代码
2018/02/09 Python
Python基于FTP模块实现ftp文件上传操作示例
2018/04/23 Python
python面试题Python2.x和Python3.x的区别
2019/05/28 Python
python库matplotlib绘制坐标图
2019/10/18 Python
Python Pickle 实现在同一个文件中序列化多个对象
2019/12/30 Python
Python异常继承关系和自定义异常实现代码实例
2020/02/20 Python
联想中国官方商城:Lenovo China
2017/10/18 全球购物
2014小学植树节活动总结
2014/03/10 职场文书
业务员自荐信范文
2014/04/20 职场文书
提拔干部考察材料
2014/05/26 职场文书
2014年环保工作总结
2014/11/26 职场文书
现役军人家属慰问信
2015/03/24 职场文书
体育部部长竞选稿
2015/11/21 职场文书
2016年圣诞节寄语(一句话)
2015/12/07 职场文书