Maison > Tutoriel CMS > WordPresse > Partager un code de navigation du fil d'Ariane WordPress

Partager un code de navigation du fil d'Ariane WordPress

藏色散人
Libérer: 2021-05-06 10:13:33
avant
3025 Les gens l'ont consulté

Ce qui suit est un code de navigation du fil d'Ariane WordPress partagé par la colonne du didacticiel WordPress. J'espère qu'il sera utile aux amis qui en ont besoin !

Réimprimez et partagez un morceau de code de navigation du fil d'Ariane WordPress qui prend en charge les types de publication personnalisés et les catégories personnalisées, mais il semble dommage que la hiérarchie des catégories parent-enfant ne puisse pas être affichée sur la page d'archive des catégories.

Ajoutez le code au modèle de fonction de thème actuel function.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;;  
    }
}
Copier après la connexion

Placez le code d'appel à l'emplacement approprié du modèle de thème, tel que header.php :

<?php if (function_exists(&#39;tsh_wp_custom_breadcrumbs&#39;)) tsh_wp_custom_breadcrumbs(); ?>
Copier après la connexion

Style assorti :

#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;
}
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal