【问题标题】:WP SEO Yoast plugin breadcrumb and JSON-LD (BreadcrumbList) integrationWP SEO Yoast 插件面包屑和 JSON-LD (BreadcrumbList) 集成
【发布时间】:2018-12-07 17:50:06
【问题描述】:

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

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

<?php 
 yoast_breadcrumb();
?>

但是,我正在尝试将 JSON-LD 模式与 Yoast Breadcrumb API 与下面的 JSON-LD 代码示例集成,但我在文档中找不到任何地方来实现这一点,API 显示 HTML 格式面包屑列表,这不是我想要的,我想要 array 格式,以便能够使用 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"
   }
  }
 ]
}

【问题讨论】:

    标签: php wordpress breadcrumbs json-ld yoast


    【解决方案1】:

    您可以过滤输出并收集您的 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多