1

我正在尝试将 yoast_breadcrumb API 与 JSON-LD 集成。

根据 SEO Yoast 插件文档,我的面包屑代码如下:

<?php 
 yoast_breadcrumb();
?>

但是,我正在尝试将 JSON-LD 模式与 Yoast Breadcrumb API 与下面的 JSON-LD 代码示例集成,但我在文档中找不到任何地方来实现这一点,API 显示面包屑列表的 HTML 格式,其中不是我想要的,我想要数组格式,以便能够使用foreach 循环构造 JSON-LD 。

{
 "@context": "http://schema.org",
 "@type": "BreadcrumbList",
 "itemListElement":
 [
  {
   "@type": "ListItem",
   "position": 1,
   "item":
   {
    "@id": "https://example.com/news/",
    "name": "News"
    }
  },
  {
   "@type": "ListItem",
  "position": 2,
  "item":
   {
     "@id": "https://example.com/news/finance/",
     "name": "Finance"
   }
  }
 ]
}
4

1 回答 1

3

您可以过滤输出并收集您的 JSON。但是,在下面提供的示例中,如果您想在文档的 head 部分中输出,“收集”可能为时已晚。然后,您可以更早地调用面包屑函数,而不用回显它并收集数据、删除过滤器并呈现 JSON。

/* echo breadcrumbs in template */
yoast_breadcrumb('<p id="breadcrumbs">','</p>');

/* collect breadcrumb whenever */
$breadcrumbs = yoast_breadcrumb('','',false);

这是过滤器功能:

add_filter('wpseo_breadcrumb_links', 'entex_add_crumb_schema', 10, 1);
function entex_add_crumb_schema($crumbs) {

    if( ! is_array( $crumbs ) || $crumbs === array()){
        return $crumbs;
    }

    $last = count($crumbs);
    $listItems = [];
    $j = 1;

    foreach ( $crumbs as $i => $crumb ) {

        $item = [];
        $nr = ($i + 1);

        if(isset($crumb['id'])){
            $item = [
                '@id' => get_permalink($crumb['id']),
                'name' => strip_tags( get_the_title( $id ) )
            ];
        }

        if(isset($crumb['term'])){
            $term = $crumb['term'];

            $item = [
                '@id' => get_term_link( $term ),
                'name' => $term->name
            ];
        }

        if(isset($crumb['ptarchive'])){
            $postType = get_post_type_object($crumb['ptarchive']);

            $item = [
                '@id' => get_post_type_archive_link($crumb['ptarchive']),
                'name' => $postType->label
            ];
        }

        /* READ NOTE BELOW: */

        if($nr == $last){
            if(is_author() && !isset($crumb['url'])) $crumb['url'] = esc_url(get_author_posts_url(get_queried_object_id()));
        }

        /* The 'text' indicates the current (last) or start-path crumb (home)*/
        if(isset($crumb['url'])) {
            if($crumb['text'] !== '') {
                $title = $crumb['text'];
            } else {
                $title = get_bloginfo('name');
            }

            $item = [
                '@id' => $crumb['url'],
                'name' => $title
            ];
        }

        $listItem = [
            '@type' => 'ListItem',
            'position' => $j,
            'item' => $item
        ];

        $listItems[] = $listItem;
        $j++;
    }

    $schema = [
        '@context' => 'http://schema.org',
        '@type' => 'BreadcrumbList',
        'itemListElement' => $listItems
    ];

    $html = '<script type="application/ld+json">' . stripslashes(json_encode($schema, JSON_PRETTY_PRINT)) . '</script> ';
    echo $html;
    remove_filter('wpseo_breadcrumb_links', 'entex_add_crumb_schema', 10, 1);
    return $crumbs;
}

(*) 注意 Yoast 不会填充当前登录页面/存档登录页面的 URL。您必须在函数中为作者存档添加示例。这取决于您是否想要当前路径在模式中,因此我将在每个用户案例中对其进行修改。

(*) 提示 这是 RAW 示例。对填充的变量进行一些清理,以避免 javascript 范围问题。此外,仅当您使用 PRETTY 参数时才需要最后的 stripslashes。

快乐的 JSON

于 2017-10-13T04:14:34.317 回答