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 MySQL与分页效率
Jun 04 PHP
php面向对象的方法重载两种版本比较
Sep 08 PHP
PHP HTML代码串截取代码
Dec 29 PHP
如何使用Linux的Crontab定时执行PHP脚本的方法
Dec 19 PHP
php开发文档 会员收费1期
Aug 14 PHP
php获取通过http协议post提交过来xml数据及解析xml
Dec 16 PHP
php调用mysql存储过程实例分析
Dec 29 PHP
PHP实现支持SSL连接的SMTP邮件发送类
Mar 05 PHP
php操作access数据库的方法详解
Feb 22 PHP
PHP网站自动化配置的实现方法(必看)
May 27 PHP
关于php支持的协议与封装协议总结(推荐)
Nov 17 PHP
Laravel如何使用Redis共享Session
Feb 23 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中文本数据翻页(留言本翻页)
2006/10/09 PHP
PHP 5.0 Pear安装方法
2006/12/06 PHP
PHP Ajax中文乱码问题解决方法
2009/02/27 PHP
PHP5 面向对象(学习记录)
2009/12/02 PHP
《PHP编程最快明白》第二讲 数字、浮点、布尔型、字符串和数组
2010/11/01 PHP
php中根据变量的类型 选择echo或dump
2012/07/05 PHP
Ajax+PHP快速上手及简单应用说明
2013/07/24 PHP
鼠标事件的screenY,pageY,clientY,layerY,offsetY属性详解
2015/03/12 Javascript
如何解决IONIC页面底部被遮住无法向上滚动问题
2016/09/06 Javascript
JS基于面向对象实现的拖拽功能示例
2016/12/20 Javascript
vue.js获取数据库数据实例代码
2017/05/26 Javascript
vue中设置height:100%无效的问题及解决方法
2018/07/27 Javascript
详解微信JS-SDK选择图片遇到的坑
2018/08/15 Javascript
vue+axios 前端实现的常用拦截的代码示例
2018/08/23 Javascript
vue axios请求频繁时取消上一次请求的方法
2018/11/10 Javascript
uin-app+mockjs实现本地数据模拟
2020/08/26 Javascript
js实现鼠标拖曳效果
2020/12/30 Javascript
Python while 循环使用的简单实例
2016/06/08 Python
将python运行结果保存至本地文件中的示例讲解
2019/07/11 Python
用django设置session过期时间的方法解析
2019/08/05 Python
pytorch 指定gpu训练与多gpu并行训练示例
2019/12/31 Python
将labelme格式数据转化为标准的coco数据集格式方式
2020/02/17 Python
python Shapely使用指南详解
2020/02/18 Python
Python telnet登陆功能实现代码
2020/04/16 Python
python相对企业语言优势在哪
2020/06/12 Python
美国高端婴童品牌:Hanna Andersson
2016/10/30 全球购物
Jacques Lemans德国:奥地利钟表品牌
2019/12/26 全球购物
某IT外企面试题-二分法求方程!看看大家的C++功底
2015/07/04 面试题
社区消防工作实施方案
2014/03/21 职场文书
《毛主席在花山》教学反思
2014/04/20 职场文书
《当代神农氏》教学反思
2016/02/23 职场文书
Django实现聊天机器人
2021/05/31 Python
用python批量解压带密码的压缩包
2021/05/31 Python
Python帮你解决手机qq微信内存占用太多问题
2022/02/15 Python
GTX1660显卡搭配显示器推荐
2022/04/19 数码科技
JDK8中String的intern()方法实例详细解读
2022/09/23 Java/Android