WP SEO Yoast插件面包屑和JSON-LD(BreadcrumbList)集成 [英] WP SEO Yoast plugin breadcrumb and JSON-LD (BreadcrumbList) integration

查看:196
本文介绍了WP SEO Yoast插件面包屑和JSON-LD(BreadcrumbList)集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to integrate yoast_breadcrumb API with JSON-LD.

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

According to the SEO Yoast plugin documentation, I have this breadcrumb code as below:

<?php 
 yoast_breadcrumb();
?>

但是,我试图通过下面的JSON-LD代码示例将JSON-LD模式与Yoast Breadcrumb API集成在一起,但是我在文档中找不到任何地方可以实现此目的,该API显示了HTML格式的BreadcrumbList,这不是我想要的,我想要具有 array 格式,以便能够使用 foreach循环来构造JSON-LD.

However, I am trying to integrate JSON-LD schema with the Yoast Breadcrumb API with follow JSON-LD code example below, and I couldn't find anywhere in the documentation to achieve this, the API displays the HTML format of the breadcrumbList, which is not what I want, I want to have array format so that I would be able to construct the JSON-LD using foreach loop.

{
 "@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"
   }
  }
 ]
}

推荐答案

您可以过滤输出并收集JSON.但是,在下面提供的示例中,如果要在文档的开头部分中输出,则收集"可能会很晚.然后,您可以更早地调用面包屑函数,而无需回显它并收集数据,删除过滤器并呈现JSON.

You can filter the output and collect your JSON. However, in provided example below, the "collect" is maybe to late if you wanna output within the head section of the document. You can then call the breadcrumb function earlier, without echo it and collect the data, remove the filter and render the 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填充到当前登录页面/归档登录页面.您必须在函数中为作者档案添加示例.这取决于您是否要在架构中使用当前路径,因此我将在每个用户案例中对其进行修改.

(*) NOTE Yoast doesnt populate the urls to current landing-pages/ archive landing pages. You have to add those with the example for the author archive in the function. This depends if you want the current trail or not in the schema, so I leave this to be modified at each user case.

(*)提示 这是RAW示例.对您填充的var进行一些检查,以避免javascript范围问题.另外,仅当使用PRETTY参数时,才需要最后一个反斜杠.

(*) TIPS This is RAW examples. Do some sanitazion on your populated vars, to avoid javascript scope issues. Also the last stripslashes is only needed if you use PRETTY arguments.

快乐JSON

这篇关于WP SEO Yoast插件面包屑和JSON-LD(BreadcrumbList)集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆