ホームページ  >  記事  >  CMS チュートリアル  >  WordPress ブレッドクラム ナビゲーション コードを共有する

WordPress ブレッドクラム ナビゲーション コードを共有する

藏色散人
藏色散人転載
2021-05-06 10:13:332923ブラウズ

以下は、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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はzmingcx.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。