免费发布信息
微信公众号
当前位置: 首页 » 帮助中心 » 常见问题 » 正文

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

   来源:黔优网时间:2024-09-23 12:01:47 浏览量:0

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

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

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

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面包屑导航代码的详细内容,更多请关注本网内其它相关文章!

 
 
没用 0举报 收藏 0
免责声明:
黔优网以上展示内容来源于用户自主上传、合作媒体、企业机构或网络收集整理,版权争议与本站无关,文章涉及见解与观点不代表黔优网官方立场,请读者仅做参考。本文标题:分享一个WordPress面包屑导航代码,本文链接:https://www.qianu.com/help/51223.html,欢迎转载,转载时请说明出处。若您认为本文侵犯了您的版权信息,或您发现该内容有任何违法信息,请您立即点此【投诉举报】并提供有效线索,也可以通过邮件(邮箱号:kefu@qianu.com)联系我们及时修正或删除。
 
 

 

 
推荐图文
推荐帮助中心