【问题标题】:full path in breadcrumbs wordpress面包屑wordpress中的完整路径
【发布时间】:2018-02-25 13:37:11
【问题描述】:

我想在wordpress模板中用面包屑的完整路径

现在只显示主页和类别

我想要主页 - 类别 - 带有指向此页面链接的文章标题,以及有效的架构标记

现在代码看起来像这样 版本 Wordpress 4.8.1

function barcelona_breadcrumb() {
    if ( barcelona_get_option( 'show_breadcrumb' ) != 'on' ) {
        return;
    }
    $barcelona_post_type = is_single() ? get_post_type() : NULL;
    $barcelona_sep_icon = '';
    $barcelona_items = '';
    if ( ( is_single() && $barcelona_post_type == 'post' && ! is_attachment() ) || is_category() ) {
        $barcelona_categories = is_category() ? array() : get_the_category();
        $barcelona_current_cat = $barcelona_last_cat = is_category() ? get_queried_object() : $barcelona_categories[0];
        $barcelona_counter = 3;
        while ( $barcelona_current_cat->category_parent != '0' ) {
            $barcelona_current_cat = get_category( $barcelona_current_cat->category_parent );
            $barcelona_items = $barcelona_sep_icon .'<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a itemprop="item" href="'. esc_url( get_category_link( $barcelona_current_cat ) ) .'"><span itemprop="name">'. esc_html( $barcelona_current_cat->name ) .'</span></a><meta itemprop="position" content="%'. $barcelona_counter .'%" /></li>'. $barcelona_items;
            $barcelona_counter++;
        }
        $barcelona_items .= $barcelona_sep_icon .'<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><'. ( is_category() ? 'span' : 'a href="'. esc_url( get_category_link( $barcelona_last_cat ) ) .'"' ) .' itemprop="item"><span itemprop="name">'. esc_html( $barcelona_last_cat->name ) .'</span></'. ( is_category() ? 'span' : 'a' ) .'><meta itemprop="position" content="%2%" /></li>';
            if ( $barcelona_counter > 3 ) {
            $barcelona_arr = array_reverse( range( 2, $barcelona_counter - 1 ) );
            foreach( $barcelona_arr as $k => $v ) {
                $barcelona_items = str_replace( 'itemprop="position" content="%'. intval( $k + 2 ) .'%"', 'itemprop="position" content="'. intval( $v ) .'"', $barcelona_items );
            }
        } else {
            $barcelona_items = str_replace( 'content="%2%"', 'content="2"', $barcelona_items );
        }
    } else if ( is_archive() || is_search() ) {
        $barcelona_title = is_search() ? esc_html__( 'Search Results', 'barcelona' ) : esc_html( get_the_archive_title() );
        if ( is_author() ) {
            $barcelona_title = esc_html__( 'Author Archive', 'barcelona' );
        } else if ( is_year() ) {
            $barcelona_title = esc_html__( 'Yearly Archive', 'barcelona' );
        } else if ( is_month() ) {
            $barcelona_title = esc_html__( 'Monthly Archive', 'barcelona' );
        } else if ( is_day() ) {
            $barcelona_title = esc_html__( 'Daily Archive', 'barcelona' );
        } else if ( is_tag() ) {
            $barcelona_title = esc_html__( 'Tag Archive', 'barcelona' );
        }
        $barcelona_items .= $barcelona_sep_icon .'<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="item"><span itemprop="name">'. $barcelona_title .'</span></span><meta itemprop="position" content="2" /></li>';
    }
    $barcelona_items = '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a itemprop="item" href="'. esc_url( home_url( '/' ) ) .'">'. esc_html__( 'Home', 'barcelona' ) .'</a><meta itemprop="position" content="1" /></li>'. $barcelona_items;
        echo '<div class="breadcrumb-wrapper"><div class="container"><ol itemscope itemtype="http://schema.org/BreadcrumbList" class="breadcrumb">'. $barcelona_items .'</ol></div></div>';
}

谢谢!

【问题讨论】:

    标签: php wordpress schema breadcrumbs


    【解决方案1】:

    试试下面的代码,把下面的代码放在functions.php中

    function the_breadcrumbs() {
    
        global $post;
    
        if (!is_home()) {
    
            echo "<a href='";
            echo get_option('home');
            echo "'>";
            echo "Site's name here";
            echo "</a>";
    
            if (is_category() || is_single()) {
    
                echo " > ";
                $cats = get_the_category( $post->ID );
    
                foreach ( $cats as $cat ){
                    echo $cat->cat_name;
                    echo " > ";
                }
                if (is_single()) {
                    the_title();
                }
            } elseif (is_page()) {
    
                if($post->post_parent){
                    $anc = get_post_ancestors( $post->ID );
                    $anc_link = get_page_link( $post->post_parent );
    
                    foreach ( $anc as $ancestor ) {
                        $output = " > <a href=".$anc_link.">".get_the_title($ancestor)."</a> > ";
                    }
    
                    echo $output;
                    the_title();
    
                } else {
                    echo ' > ';
                    echo the_title();
                }
            }
        }
    elseif (is_tag()) {single_tag_title();}
    elseif (is_day()) {echo"Archive: "; the_time('F jS, Y'); echo'</li>';}
    elseif (is_month()) {echo"Archive: "; the_time('F, Y'); echo'</li>';}
    elseif (is_year()) {echo"Archive: "; the_time('Y'); echo'</li>';}
    elseif (is_author()) {echo"Author's archive: "; echo'</li>';}
    elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "Blogarchive: "; echo'';}
    elseif (is_search()) {echo"Search results: "; }
    }
    

    将下面的代码放在 header.php 中

     if(function_exists('the_breadcrumbs')) the_breadcrumbs();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多