Rumah > Tutorial CMS > WordTekan > 分享一个WordPress面包屑导航代码

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

藏色散人
Lepaskan: 2021-05-06 10:13:33
ke hadapan
2959 orang telah melayarinya

下面由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;;  
    }
}
Salin selepas log masuk

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

<?php if (function_exists(&#39;tsh_wp_custom_breadcrumbs&#39;)) tsh_wp_custom_breadcrumbs(); ?>
Salin selepas log masuk

配套样式:

#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;
}
Salin selepas log masuk

Atas ialah kandungan terperinci 分享一个WordPress面包屑导航代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:zmingcx.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan