問題描述

我正在使用 Yoast WordPress SEO,我已經設置了我的麪包屑。問題是我的頁面設置如下。

/
/about
/blog - On this page I query the posts and display them. The posts themselves have nothing before them in the URL.

麪包屑如下所示。

Home / Category / Page Title

我想要這樣顯示。

Home/ Blog / Category / Page Title

這可能嗎?

最佳解決方案

以下是您需要做的一般原則:

  1. 鈎入 wpseo_breadcrumb_linkswp_seo_get_bc_ancestors API filters

  2. 將您的博客添加到 WordPress SEO 麪包屑 $links 陣列中,使用 array_splice

將其放在主題的 functions.php 中:

/**
 * Conditionally Override Yoast SEO Breadcrumb Trail
 * http://plugins.svn.wordpress.org/wordpress-seo/trunk/frontend/class-breadcrumbs.php
 * -----------------------------------------------------------------------------------
 */

add_filter( 'wpseo_breadcrumb_links', 'wpse_100012_override_yoast_breadcrumb_trail' );

function wpse_100012_override_yoast_breadcrumb_trail( $links ) {
    global $post;

    if ( is_home() || is_singular( 'post' ) || is_archive() ) {
        $breadcrumb[] = array(
            'url' => get_permalink( get_option( 'page_for_posts' ) ),
            'text' => 'Blog',
        );

        array_splice( $links, 1, -2, $breadcrumb );
    }

    return $links;
}

注意:您可能需要更新特定於您網站或需求的代碼,但總體思路保持不變。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。