登录  /  注册
首页 > CMS教程 > WordPress > 正文

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

藏色散人
发布: 2021-05-06 10:13:33
转载
2702人浏览过

下面由wordpress/" target="_blank">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 &#39;<ul id="&#39; . $breadcrumbs_id . &#39;" class="&#39; . $breadcrumbs_class . &#39;">&#39;;
           
        // Home
        echo &#39;<li class="item-home"><a class="bread-link bread-home" href="&#39; . get_home_url() . &#39;" title="&#39; . $home_title . &#39;">&#39; . $home_title . &#39;</a></li>&#39;;
        echo &#39;<li class="separator separator-home"> &#39; . $separator . &#39; </li>&#39;;
           
        if ( is_archive() && !is_tax() && !is_category() && !is_tag() ) {
              
            echo &#39;<li class="item-current item-archive"><strong class="bread-current bread-archive">&#39; . post_type_archive_title(&#39;&#39;, false) . &#39;</strong></li>&#39;;
              
        } 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 != &#39;post&#39;) {
                  
                $post_type_object = get_post_type_object($post_type);
                $post_type_archive = get_post_type_archive_link($post_type);
              
                echo &#39;<li class="item-cat item-custom-post-type-&#39; . $post_type . &#39;"><a class="bread-cat bread-custom-post-type-&#39; . $post_type . &#39;" href="&#39; . $post_type_archive . &#39;" title="&#39; . $post_type_object->labels->name . &#39;">&#39; . $post_type_object->labels->name . &#39;</a></li>&#39;;
                echo &#39;<li class="separator"> &#39; . $separator . &#39; </li>&#39;;
              
            }
              
            $custom_tax_name = get_queried_object()->name;
            echo &#39;<li class="item-current item-archive"><strong class="bread-current bread-archive">&#39; . $custom_tax_name . &#39;</strong></li>&#39;;
              
        } else if ( is_single() ) {
              
            $post_type = get_post_type();
 
            if($post_type != &#39;post&#39;) {
                  
                $post_type_object = get_post_type_object($post_type);
                $post_type_archive = get_post_type_archive_link($post_type);
              
                echo &#39;<li class="item-cat item-custom-post-type-&#39; . $post_type . &#39;"><a class="bread-cat bread-custom-post-type-&#39; . $post_type . &#39;" href="&#39; . $post_type_archive . &#39;" title="&#39; . $post_type_object->labels->name . &#39;">&#39; . $post_type_object->labels->name . &#39;</a></li>&#39;;
                echo &#39;<li class="separator"> &#39; . $separator . &#39; </li>&#39;;
              
            }
              
            // 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, &#39;,&#39;),&#39;,&#39;);
                $cat_parents = explode(&#39;,&#39;,$get_cat_parents);
                  
                // Loop through parent categories and store in variable $cat_display
                $cat_display = &#39;&#39;;
                foreach($cat_parents as $parents) {
                    $cat_display .= &#39;<li class="item-cat">&#39;.$parents.&#39;</li>&#39;;
                    $cat_display .= &#39;<li class="separator"> &#39; . $separator . &#39; </li>&#39;;
                }
             
            }
 
            $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 &#39;<li class="item-current item-&#39; . $post->ID . &#39;"><strong class="bread-current bread-&#39; . $post->ID . &#39;" title="&#39; . get_the_title() . &#39;">&#39; . get_the_title() . &#39;</strong></li>&#39;;
                  
            // Post is in a custom taxonomy
            } else if(!empty($cat_id)) {
                  
                echo &#39;<li class="item-cat item-cat-&#39; . $cat_id . &#39; item-cat-&#39; . $cat_nicename . &#39;"><a class="bread-cat bread-cat-&#39; . $cat_id . &#39; bread-cat-&#39; . $cat_nicename . &#39;" href="&#39; . $cat_link . &#39;" title="&#39; . $cat_name . &#39;">&#39; . $cat_name . &#39;</a></li>&#39;;
                echo &#39;<li class="separator"> &#39; . $separator . &#39; </li>&#39;;
                echo &#39;<li class="item-current item-&#39; . $post->ID . &#39;"><strong class="bread-current bread-&#39; . $post->ID . &#39;" title="&#39; . get_the_title() . &#39;">&#39; . get_the_title() . &#39;</strong></li>&#39;;
              
            } else {
                  
                echo &#39;<li class="item-current item-&#39; . $post->ID . &#39;"><strong class="bread-current bread-&#39; . $post->ID . &#39;" title="&#39; . get_the_title() . &#39;">&#39; . get_the_title() . &#39;</strong></li>&#39;;
                  
            }
              
        } else if ( is_category() ) {
               
            // Category page
            echo &#39;<li class="item-current item-cat"><strong class="bread-current bread-cat">&#39; . single_cat_title(&#39;&#39;, false) . &#39;</strong></li>&#39;;
               
        } 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 .= &#39;<li class="item-parent item-parent-&#39; . $ancestor . &#39;"><a class="bread-parent bread-parent-&#39; . $ancestor . &#39;" href="&#39; . get_permalink($ancestor) . &#39;" title="&#39; . get_the_title($ancestor) . &#39;">&#39; . get_the_title($ancestor) . &#39;</a></li>&#39;;
                    $parents .= &#39;<li class="separator separator-&#39; . $ancestor . &#39;"> &#39; . $separator . &#39; </li>&#39;;
                }
                   
                // Render parent pages
                echo $parents;
                   
                // Active page
                echo &#39;<li class="item-current item-&#39; . $post->ID . &#39;"><strong title="&#39; . get_the_title() . &#39;"> &#39; . get_the_title() . &#39;</strong></li>&#39;;
                   
            } else {
                   
                // Just display active page if not parents pages
                echo &#39;<li class="item-current item-&#39; . $post->ID . &#39;"><strong class="bread-current bread-&#39; . $post->ID . &#39;"> &#39; . get_the_title() . &#39;</strong></li>&#39;;
                   
            }
               
        } else if ( is_tag() ) { // Tag page
               
            // Tag information
            $term_id        = get_query_var(&#39;tag_id&#39;);
            $taxonomy       = &#39;post_tag&#39;;
            $args           = &#39;include=&#39; . $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 &#39;<li class="item-current item-tag-&#39; . $get_term_id . &#39; item-tag-&#39; . $get_term_slug . &#39;"><strong class="bread-current bread-tag-&#39; . $get_term_id . &#39; bread-tag-&#39; . $get_term_slug . &#39;">&#39; . $get_term_name . &#39;</strong></li>&#39;;
           
        } elseif ( is_day() ) { // Day archive page
               
            // Year link
            echo &#39;<li class="item-year item-year-&#39; . get_the_time(&#39;Y&#39;) . &#39;"><a class="bread-year bread-year-&#39; . get_the_time(&#39;Y&#39;) . &#39;" href="&#39; . get_year_link( get_the_time(&#39;Y&#39;) ) . &#39;" title="&#39; . get_the_time(&#39;Y&#39;) . &#39;">&#39; . get_the_time(&#39;Y&#39;) . &#39; Archives</a></li>&#39;;
            echo &#39;<li class="separator separator-&#39; . get_the_time(&#39;Y&#39;) . &#39;"> &#39; . $separator . &#39; </li>&#39;;
               
            // Month link
            echo &#39;<li class="item-month item-month-&#39; . get_the_time(&#39;m&#39;) . &#39;"><a class="bread-month bread-month-&#39; . get_the_time(&#39;m&#39;) . &#39;" href="&#39; . get_month_link( get_the_time(&#39;Y&#39;), get_the_time(&#39;m&#39;) ) . &#39;" title="&#39; . get_the_time(&#39;M&#39;) . &#39;">&#39; . get_the_time(&#39;M&#39;) . &#39; Archives</a></li>&#39;;
            echo &#39;<li class="separator separator-&#39; . get_the_time(&#39;m&#39;) . &#39;"> &#39; . $separator . &#39; </li>&#39;;
               
            // Day display
            echo &#39;<li class="item-current item-&#39; . get_the_time(&#39;j&#39;) . &#39;"><strong class="bread-current bread-&#39; . get_the_time(&#39;j&#39;) . &#39;"> &#39; . get_the_time(&#39;jS&#39;) . &#39; &#39; . get_the_time(&#39;M&#39;) . &#39; Archives</strong></li>&#39;;
               
        } else if ( is_month() ) { // Month Archive
               
            // Year link
            echo &#39;<li class="item-year item-year-&#39; . get_the_time(&#39;Y&#39;) . &#39;"><a class="bread-year bread-year-&#39; . get_the_time(&#39;Y&#39;) . &#39;" href="&#39; . get_year_link( get_the_time(&#39;Y&#39;) ) . &#39;" title="&#39; . get_the_time(&#39;Y&#39;) . &#39;">&#39; . get_the_time(&#39;Y&#39;) . &#39; Archives</a></li>&#39;;
            echo &#39;<li class="separator separator-&#39; . get_the_time(&#39;Y&#39;) . &#39;"> &#39; . $separator . &#39; </li>&#39;;
               
            // Month display
            echo &#39;<li class="item-month item-month-&#39; . get_the_time(&#39;m&#39;) . &#39;"><strong class="bread-month bread-month-&#39; . get_the_time(&#39;m&#39;) . &#39;" title="&#39; . get_the_time(&#39;M&#39;) . &#39;">&#39; . get_the_time(&#39;M&#39;) . &#39; Archives</strong></li>&#39;;
               
        } else if ( is_year() ) { // Display year archive
 
            echo &#39;<li class="item-current item-current-&#39; . get_the_time(&#39;Y&#39;) . &#39;"><strong class="bread-current bread-current-&#39; . get_the_time(&#39;Y&#39;) . &#39;" title="&#39; . get_the_time(&#39;Y&#39;) . &#39;">&#39; . get_the_time(&#39;Y&#39;) . &#39; Archives</strong></li>&#39;;
               
        } else if ( is_author() ) { // Author archive
               
            // Get the author information
            global $author;
            $userdata = get_userdata( $author );
               
            // Display author name
            echo &#39;<li class="item-current item-current-&#39; . $userdata->user_nicename . &#39;"><strong class="bread-current bread-current-&#39; . $userdata->user_nicename . &#39;" title="&#39; . $userdata->display_name . &#39;">&#39; . &#39;Author: &#39; . $userdata->display_name . &#39;</strong></li>&#39;;
           
        } else if ( get_query_var(&#39;paged&#39;) ) {
               
            // Paginated archives
            echo &#39;<li class="item-current item-current-&#39; . get_query_var(&#39;paged&#39;) . &#39;"><strong class="bread-current bread-current-&#39; . get_query_var(&#39;paged&#39;) . &#39;" title="Page &#39; . get_query_var(&#39;paged&#39;) . &#39;">&#39;.__(&#39;Page&#39;) . &#39; &#39; . get_query_var(&#39;paged&#39;) . &#39;</strong></li>&#39;;
               
        } else if ( is_search() ) {
           
            // Search results page
            echo &#39;<li class="item-current item-current-&#39; . get_search_query() . &#39;"><strong class="bread-current bread-current-&#39; . get_search_query() . &#39;" title="Search results for: &#39; . get_search_query() . &#39;">Search results for: &#39; . get_search_query() . &#39;</strong></li>&#39;;
           
        } elseif ( is_404() ) {
               
            // 404 page
            echo &#39;<li>&#39; . &#39;Error 404&#39; . &#39;</li>&#39;;
        }
 
        echo &#39;</ul>&#39;;  
    }
}
登录后复制

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

<?php if (function_exists(&#39;tsh_wp_custom_breadcrumbs&#39;)) 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;
}
登录后复制

以上就是分享一个WordPress面包屑导航代码的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:zmingcx网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号