2016-12-09 19 views
1

yoast_breadcrumb APIをJSON-LDと統合しようとしています。WP SEO YoastプラグインのブレッドクラムとJSON-LD(ブレッドクラムリスト)の統合

SEO Yoastプラグインのドキュメントによると、私は以下のように、このブレッドクラムコードを持っている:

<?php 
yoast_breadcrumb(); 
?> 

しかし、私がフォローJSON-LDのコード例でYoastブレッドクラムのAPIとJSON-LDのスキーマを統合しようとしています私はこれを達成するためにドキュメントのどこにも見つけることができませんでした。APIはbreadcrumbListのHTML形式を表示します。これは私が望むものではありません。の配列の形式を持っています。 JSON-LD foreachループを使用します。

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

答えて

1

出力をフィルタリングしてJSONを収集することができます。ただし、以下の例では、文書の先頭部分に出力する場合は、「収集」が遅れることがあります。その後、ブレッドクラム機能をエコーすることなくブレーククラム機能を呼び出して、データを収集し、フィルタを削除して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; 
} 

(*)NOTE Yoastは、現在のランディングページ/アーカイブランディングページへのURLを移入doesntの。関数内の著者アーカイブの例を持つものを追加する必要があります。これは、スキーマに現在のトレイルを表示するかどうかによって異なります。したがって、各ユーザーのケースでこれを変更したままにしておきます。

(*)TIPS これはRAWの例です。 javascriptの範囲の問題を避けるために、あなたの人口密度分布にいくつかのsanitazionを行います。また、最後のストライプラッシュは、PRETTY引数を使用する場合にのみ必要です。

ハッピーJSON

関連する問題