douli4337 2017-11-16 15:02
浏览 77
已采纳

如何在JSON-LD中包含WordPress PHP标记?

I want to add JSON-LD structured data to my WordPress WooCommerce site in order to increase my chances of rich snippets on search engine results pages.

I want to begin by adding it first to my products pages.

If I've included the code below in header-shop.php...

    <!-- Include Schema Markup File
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<?php include('json-ld.php'); ?><script type="application/ld+json"><?php echo json_encode($payload); ?></script>

how do I reference PHP tags like <php the_title(); ?> within the json-ld.php file?

Is it as simple as the following?

<script type="application/ld+json">
{
  "@context": "http://schema.org/",
  "@type": "Product",
  "name": "<?php the_title(); ?>",
  "image": [
    "https://example.com/photos/1x1/photo.jpg",
    "https://example.com/photos/4x3/photo.jpg",
    "https://example.com/photos/16x9/photo.jpg"
   ],
  "brand": {
    "@type": "Thing",
    "name": "ACME"
  },
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": "4.4",
    "ratingCount": "89"
  },
  "offers": {
    "@type": "AggregateOffer",
    "lowPrice": "119.99",
    "highPrice": "199.99",
    "priceCurrency": "USD"
  }
}
</script>
  • 写回答

1条回答 默认 最新

  • dpvmtdu364462 2017-11-16 15:11
    关注

    You could do this in PHP to print to your page. By echoing your script in entirety will allow you to concat inline functions.

    echo '
    <script type="application/ld+json">
    {
      "@context": "http://schema.org/",
      "@type": "Product",
      "name": "'. the_title() .'",
      "image": [
        "https://example.com/photos/1x1/photo.jpg",
        "https://example.com/photos/4x3/photo.jpg",
        "https://example.com/photos/16x9/photo.jpg"
       ],
      "brand": {
        "@type": "Thing",
        "name": "ACME"
      },
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "4.4",
        "ratingCount": "89"
      },
      "offers": {
        "@type": "AggregateOffer",
        "lowPrice": "119.99",
        "highPrice": "199.99",
        "priceCurrency": "USD"
      }
    }
    </script>
    ';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置