所有分类
  • 所有分类
  • 其它

WordPress如何添加面包屑导航

面包屑导航是一系列连接的导航链接,这些链接显示了您浏览网站页面的路径,面包屑通常显示在页面顶部,以方便访问。主要目的是使用户能够轻松地回溯,从而改善站点的UX,除此以外,面包屑导航对于整体网站体验和SEO都有好处。那么WordPress如何添加面包屑导航?

WordPress添加面包屑导航

我们可以通过自定义代码实现面包屑,将以下代码添加到现用主题的functions.php文件中:

    1. // WordPress Breadcrumb Function
    2. // Add this code into your theme function file.
    3. function ah_breadcrumb() {
    4. // Check if is front/home page, return
    5. if ( is_front_page() ) {
    6. return;
    7. }
    8. // Define
    9. global $post;
    10. $custom_taxonomy = ''; // If you have custom taxonomy place it here
    11. $defaults = array(
    12. 'seperator' => '»',
    13. 'id' => 'ah-breadcrumb',
    14. 'classes' => 'ah-breadcrumb',
    15. 'home_title' => esc_html__( 'Home', '' )
    16. );
    17. $sep = '
    18.  

      . esc_html( $defaults['seperator'] ) .'

;

    1. // Start the breadcrumb with a link to your homepage
    2. echo '

        .

       

      esc_attr( $defaults['id'] ) .'" class="'. esc_attr( $defaults['classes'] ) .'">';

    3. // Creating home link
    4. echo '
    5. .
    6.  

      get_home_url() .'">'. esc_html( $defaults['home_title'] ) .'

. $sep;

    1. if ( is_single() ) {
    2. // Get posts type
    3. $post_type = get_post_type();
    4. // If post type is not post
    5. if( $post_type != 'post' ) {
    6. $post_type_object = get_post_type_object( $post_type );
    7. $post_type_link = get_post_type_archive_link( $post_type );
    8. echo '
    9. .
    10.  

      $post_type_link .'">'. $post_type_object->labels->name .'

. $sep;

    1. }
    2. // Get categories
    3. $category = get_the_category( $post->ID );
    4. // If category not empty
    5. if( !empty( $category ) ) {
    6. // Arrange category parent to child
    7. $category_values = array_values( $category );
    8. $get_last_category = end( $category_values );
    9. // $get_last_category = $category[count($category) - 1];
    10. $get_parent_category = rtrim( get_category_parents( $get_last_category->term_id, true, ',' ), ',' );
    11. $cat_parent = explode( ',', $get_parent_category );
    12. // Store category in $display_category
    13. $display_category = '';
    14. foreach( $cat_parent as $p ) {
    15. $display_category .= '
    16.  

      . $p .'

. $sep;

    1. }
    2. }
    3. // If it's a custom post type within a custom taxonomy
    4. $taxonomy_exists = taxonomy_exists( $custom_taxonomy );
    5. if( empty( $get_last_category ) && !empty( $custom_taxonomy ) && $taxonomy_exists ) {
    6. $taxonomy_terms = get_the_terms( $post->ID, $custom_taxonomy );
    7. $cat_id = $taxonomy_terms[0]->term_id;
    8. $cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy);
    9. $cat_name = $taxonomy_terms[0]->name;
    10. }
    11. // Check if the post is in a category
    12. if( !empty( $get_last_category ) ) {
    13. echo $display_category;
    14. echo '
    15.  

      . get_the_title() .'

;

    1. } else if( !empty( $cat_id ) ) {
    2. echo '
    3. .
    4.  

      $cat_link .'">'. $cat_name .'

. $sep;

    1. echo '
    2.  

      . get_the_title() .'

;

    1. } else {
    2. echo '
    3.  

      . get_the_title() .'

;

    1. }
    2. } else if( is_archive() ) {
    3. if( is_tax() ) {
    4. // Get posts type
    5. $post_type = get_post_type();
    6. // If post type is not post
    7. if( $post_type != 'post' ) {
    8. $post_type_object = get_post_type_object( $post_type );
    9. $post_type_link = get_post_type_archive_link( $post_type );
    10. echo '
    11. .
    12.  

      $post_type . '">. $post_type_link . '">' . $post_type_object->labels->name . '

. $sep;

    1. }
    2. $custom_tax_name = get_queried_object()->name;
    3. echo '
    4.  

      . $custom_tax_name .'

;

    1. } else if ( is_category() ) {
    2. $parent = get_queried_object()->category_parent;
    3. if ( $parent !== 0 ) {
    4. $parent_category = get_category( $parent );
    5. $category_link = get_category_link( $parent );
    6. echo '
    7. .
    8.  

      esc_url( $category_link ) .'">'. $parent_category->name .'

. $sep;

    1. }
    2. echo '
    3.  

      . single_cat_title( '', false ) .'

;

    1. } else if ( is_tag() ) {
    2. // Get tag information
    3. $term_id = get_query_var('tag_id');
    4. $taxonomy = 'post_tag';
    5. $args = 'include=' . $term_id;
    6. $terms = get_terms( $taxonomy, $args );
    7. $get_term_name = $terms[0]->name;
    8. // Display the tag name
    9. echo '
    10.  

      . $get_term_name .'

;

    1. } else if( is_day() ) {
    2. // Day archive
    3. // Year link
    4. echo '
    5. .
    6.  

      get_year_link( get_the_time('Y') ) .'">'. get_the_time('Y') . ' Archives

. $sep;

    1. // Month link
    2. echo '
    3. .
    4.  

      get_month_link( get_the_time('Y'), get_the_time('m') ) .'">'. get_the_time('M') .' Archives

. $sep;

    1. // Day display
    2. echo '
    3.  

      . get_the_time('jS') .' '. get_the_time('M'). ' Archives

;

    1. } else if( is_month() ) {
    2. // Month archive
    3. // Year link
    4. echo '
    5. .
    6.  

      get_year_link( get_the_time('Y') ) .'">'. get_the_time('Y') . ' Archives

. $sep;

    1. // Month Display
    2. echo '
    3.  

      . get_the_time('M') .' Archives

;

    1. } else if ( is_year() ) {
    2. // Year Display
    3. echo '
    4.  

      . get_the_time('Y') .' Archives

;

    1. } else if ( is_author() ) {
    2. // Auhor archive
    3. // Get the author information
    4. global $author;
    5. $userdata = get_userdata( $author );
    6. // Display author name
    7. echo '
    8.  

      . 'Author: '. $userdata->display_name . '

;

    1. } else {
    2. echo '
    3.  

      . post_type_archive_title() .'

;

    1. }
    2. } else if ( is_page() ) {
    3. // Standard page
    4. if( $post->post_parent ) {
    5. // If child page, get parents
    6. $anc = get_post_ancestors( $post->ID );
    7. // Get parents in the right order
    8. $anc = array_reverse( $anc );
    9. // Parent page loop
    10. if ( !isset( $parents ) ) $parents = null;
    11. foreach ( $anc as $ancestor ) {
    12. $parents .= '
    13. .
    14.  

      get_permalink( $ancestor ) .'">'. get_the_title( $ancestor ) .'

. $sep;

    1. }
    2. // Display parent pages
    3. echo $parents;
    4. // Current page
    5. echo '
    6.  

      . get_the_title() .'

;

    1. } else {
    2. // Just display current page if not parents
    3. echo '
    4.  

      . get_the_title() .'

;

    1. }
    2. } else if ( is_search() ) {
    3. // Search results page
    4. echo '
    5. Search results for: ‘
    6.  

      . get_search_query() .'

;

    1. } else if ( is_404() ) {
    2. // 404 page
    3. echo '
    4.  

      . 'Error 404' . '

;

  1. }
  2. // End breadcrumb
  3. echo '';
  4. }

然后还需要将以下行添加到主题的header.php文件中:

  1. php
  2. // Call the breadcrumb function where you want to display
  3. if ( function_exists('ah_breadcrumb') ) ah_breadcrumb();
  4. ?>

第一个片段将面包屑添加到您的主题。第二个“调用”相关功能,以便导航链接出现在页眉中。请注意,您可能需要删除开头的<?php ,此代码才能与主题的现有文件一起使用。

显示验证码

社交账号快速登录