分享一个WordPress面包屑导航代码

大敏大大_3315

大敏大大_3315

2021-05-06

3177人浏览

转载

下面由wordpress教程栏目给大家分享一个wordpress面包屑导航代码,希望对需要的朋友有所帮助!

转载分享一段WordPress面包屑导航代码,支持自定义帖子类型、自定义分类,但貌似在分类归档页面不能显示父子分类层级有点遗憾。

将代码添加到当前主题函数模板functions.php中:

/**
 * WordPress Breadcrumbs
 */
function tsh_wp_custom_breadcrumbs() {
 
    $separator              = '/';
    $breadcrumbs_id         = 'tsh_breadcrumbs';
    $breadcrumbs_class      = 'tsh_breadcrumbs';
    $home_title             = esc_html__('Home', 'your-domain');
 
    // Add here you custom post taxonomies
    $tsh_custom_taxonomy    = 'product_cat';
 
    global $post,$wp_query;
       
    // Hide from front page
    if ( !is_front_page() ) {
       
        echo '
    ';                     // Home         echo '
  • ' . $home_title . '
  • ';         echo '
  •  ' . $separator . ' 
  • ';                     if ( is_archive() && !is_tax() && !is_category() && !is_tag() ) {                            echo '
  • ' . post_type_archive_title('', false) . '
  • ';                        } else if ( is_archive() && is_tax() && !is_category() && !is_tag() ) {                            // For Custom post type             $post_type = get_post_type();                            // Custom post type name and link             if($post_type != 'post') {                                    $post_type_object = get_post_type_object($post_type);                 $post_type_archive = get_post_type_archive_link($post_type);                                echo '
  • labels->name . '">' . $post_type_object->labels->name . '
  • ';                 echo '
  •  ' . $separator . ' 
  • ';                            }                            $custom_tax_name = get_queried_object()->name;             echo '
  • ' . $custom_tax_name . '
  • ';                        } else if ( is_single() ) {                            $post_type = get_post_type();               if($post_type != 'post') {                                    $post_type_object = get_post_type_object($post_type);                 $post_type_archive = get_post_type_archive_link($post_type);                                echo '
  • labels->name . '">' . $post_type_object->labels->name . '
  • ';                 echo '
  •  ' . $separator . ' 
  • ';                            }                            // Get post category             $category = get_the_category();                           if(!empty($category)) {                                // Last category post is in                 $last_category = $category[count($category) - 1];                                    // Parent any categories and create array                 $get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, ','),',');                 $cat_parents = explode(',',$get_cat_parents);                                    // Loop through parent categories and store in variable $cat_display                 $cat_display = '';                 foreach($cat_parents as $parents) {                     $cat_display .= '
  • '.$parents.'
  • ';                     $cat_display .= '
  •  ' . $separator . ' 
  • ';                 }                           }               $taxonomy_exists = taxonomy_exists($tsh_custom_taxonomy);             if(empty($last_category) && !empty($tsh_custom_taxonomy) && $taxonomy_exists) {                                     $taxonomy_terms = get_the_terms( $post->ID, $tsh_custom_taxonomy );                 $cat_id         = $taxonomy_terms[0]->term_id;                 $cat_nicename   = $taxonomy_terms[0]->slug;                 $cat_link       = get_term_link($taxonomy_terms[0]->term_id, $tsh_custom_taxonomy);                 $cat_name       = $taxonomy_terms[0]->name;                             }                            // If the post is in a category             if(!empty($last_category)) {                 echo $cat_display;                 echo '
  • ID . '">ID . '" title="' . get_the_title() . '">' . get_the_title() . '
  • ';                                // Post is in a custom taxonomy             } else if(!empty($cat_id)) {                                    echo '
  • ' . $cat_name . '
  • ';                 echo '
  •  ' . $separator . ' 
  • ';                 echo '
  • ID . '">ID . '" title="' . get_the_title() . '">' . get_the_title() . '
  • ';                            } else {                                    echo '
  • ID . '">ID . '" title="' . get_the_title() . '">' . get_the_title() . '
  • ';                                }                        } else if ( is_category() ) {                             // Category page             echo '
  • ' . single_cat_title('', false) . '
  • ';                         } else if ( is_page() ) {                             // Standard page             if( $post->post_parent ){                                     // Get parents                  $anc = get_post_ancestors( $post->ID );                                     // Get parents order                 $anc = array_reverse($anc);                                     // Parent pages                 if ( !isset( $parents ) ) $parents = null;                 foreach ( $anc as $ancestor ) {                     $parents .= '
  • ' . get_the_title($ancestor) . '
  • ';                     $parents .= '
  •  ' . $separator . ' 
  • ';                 }                                     // Render parent pages                 echo $parents;                                     // Active page                 echo '
  • ID . '"> ' . get_the_title() . '
  • ';                                 } else {                                     // Just display active page if not parents pages                 echo '
  • ID . '">ID . '"> ' . get_the_title() . '
  • ';                                 }                         } else if ( is_tag() ) { // Tag page                             // Tag information             $term_id        = get_query_var('tag_id');             $taxonomy       = 'post_tag';             $args           = 'include=' . $term_id;             $terms          = get_terms( $taxonomy, $args );             $get_term_id    = $terms[0]->term_id;             $get_term_slug  = $terms[0]->slug;             $get_term_name  = $terms[0]->name;                             // Return tag name             echo '
  • ' . $get_term_name . '
  • ';                     } elseif ( is_day() ) { // Day archive page                             // Year link             echo '
  • ' . get_the_time('Y') . ' Archives
  • ';             echo '
  •  ' . $separator . ' 
  • ';                             // Month link             echo '
  • ' . get_the_time('M') . ' Archives
  • ';             echo '
  •  ' . $separator . ' 
  • ';                             // Day display             echo '
  •  ' . get_the_time('jS') . ' ' . get_the_time('M') . ' Archives
  • ';                         } else if ( is_month() ) { // Month Archive                             // Year link             echo '
  • ' . get_the_time('Y') . ' Archives
  • ';             echo '
  •  ' . $separator . ' 
  • ';                             // Month display             echo '
  • ' . get_the_time('M') . ' Archives
  • ';                         } else if ( is_year() ) { // Display year archive               echo '
  • ' . get_the_time('Y') . ' Archives
  • ';                         } else if ( is_author() ) { // Author archive                             // Get the author information             global $author;             $userdata = get_userdata( $author );                             // Display author name             echo '
  • user_nicename . '">user_nicename . '" title="' . $userdata->display_name . '">' . 'Author: ' . $userdata->display_name . '
  • ';                     } else if ( get_query_var('paged') ) {                             // Paginated archives             echo '
  • '.__('Page') . ' ' . get_query_var('paged') . '
  • ';                         } else if ( is_search() ) {                         // Search results page             echo '
  • Search results for: ' . get_search_query() . '
  • ';                     } elseif ( is_404() ) {                             // 404 page             echo '
  • ' . 'Error 404' . '
  • ';         }           echo '
';       } }

将调用代码放到主题模板适当位置比如header.php中:

Wordpress OAuth
Wordpress OAuth

启动并完成 WordPress.com OAuth 认证,通过 REST API 发布文章。适用于需要生成授权链接、交换授权码等场景。

下载
<?php  if (function_exists('tsh_wp_custom_breadcrumbs')) tsh_wp_custom_breadcrumbs(); ?>

配套样式:

#tsh_breadcrumbs .separator{
    font-size:20px;
    color:#ccc;
    font-weight:100;
}
#tsh_breadcrumbs{
    overflow:hidden;
    text-align: center;
    list-style:none;
    margin:11px 0;
}
#tsh_breadcrumbs li{
    margin-right:14px;
    display:inline-block;
    vertical-align:middle;
}

相关文章

WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载

相关标签:

wordpress

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
wordpress seo
wordpress seo

WordPress网站SEO优化方法有:1、选择一个SEO友好的主题,具有清晰的代码结构,快速的加载速度和响应式设计;2、使用SEO插件,优化你的标题标签,元描述,关键字,XML站点地图等;3、优化你的内容,内容是SEO优化的核心;4、优化你的网站速度;5、创建友好的URL;6、使用内部链接;7、优化图像;8、使用社交媒体;9、定期更新你的网站;10、监控和分析你的网站等等。

2023.09.18

6475

6

wordpress下载后怎么安装
wordpress下载后怎么安装

安装前准备:确保服务器满足要求、获取安装文件、创建数据库。上传 wordpress 文件。创建数据库和用户。运行安装程序:选择语言、输入数据库信息、网站标题和管理员信息。安装 wordpress。安装后配置:设置永久链接、安装主题、安装插件、创建内容。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

2024.04.15

6539

6

WordPress插件教程
WordPress插件教程

本专题整理热门WordPress插件资源,涵盖SEO优化、缓存加速、安全防护、表单管理、电商扩展等插件推荐与使用教程。

2026.06.16

338

17

WordPress建站教程大全
WordPress建站教程大全

本专题汇总WordPress教程相关内容,包括WordPress安装教程、建站教程、使用技巧、网站管理经验和实战案例,帮助用户快速掌握WordPress建站与运营方法。

2026.06.16

840

20

WordPress开发教程
WordPress开发教程

本专题提供WordPress开发技术资料,包括主题开发、插件开发、自定义功能开发、API调用及性能优化等内容。

2026.06.16

978

25

DBeaver怎么导入数据
DBeaver怎么导入数据

DBeaver数据导入专题,整理Excel、CSV、SQL、TXT等常见文件的数据导入方法,涵盖目标数据库选择、数据表创建、字段映射、数据类型设置及导入参数配置等操作,帮助用户将本地文件中的数据写入数据库,并了解批量导入、重复数据处理和导入失败等常见问题的解决方法。

2026.11.05

0

12

DBeaver安全防护教程大全
DBeaver安全防护教程大全

DBeaver安全防护专题,围绕数据库连接安全、账号密码保护和访问权限管理展开,整理SSL/TLS加密连接、SSH隧道、数据库凭据保存及证书验证等配置方法,同时介绍敏感信息保护、远程连接风险和高权限账号使用等注意事项,帮助用户提高数据库日常管理的安全性。

2026.11.05

0

12

DBeaver连接创建与配置教程
DBeaver连接创建与配置教程

本专题聚焦DBeaver数据库连接配置与管理,覆盖从新建连接、选择数据库类型到填写连接参数和验证连接的完整流程。针对MySQL、PostgreSQL、SQL Server等常见数据库,整理相应的连接设置方法,并提供驱动缺失、认证失败和连接异常等问题的排查方向。

2026.11.04

0

16

XORM框架结构体映射与表结构同步
XORM框架结构体映射与表结构同步

本专题围绕XORM框架的结构体映射展开,说明表名与字段的命名策略、xorm标签常用属性、主键自增与时间字段自动填充,并演示Sync2自动建表、增量字段升级、索引声明与反向生成结构体的完整流程。

2026.11.04

0

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
如何在 Windows 11 上安装 XAMPP
如何在 Windows 11 上安装 XAMPP

共1课时 | 235人学习

第二十三期_前端开发
第二十三期_前端开发

共98课时 | 12万人学习