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>
    ';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 物理远程控制麦克风使用问题
  • ¥15 打印预览会泄漏纸钱包密码吗
  • ¥15 在hololens1上运行unity项目只有空窗口
  • ¥25 TABLEAU PREP无法打开
  • ¥15 百度帐号问题/centos
  • ¥15 关于#c语言#的问题:求完整代码条件好说
  • ¥100 HALCON DELPHI
  • ¥15 (需要远程,AI不回)VB6二进制文件转换成功,但是C#转换总是失败
  • ¥15 关于#matlab#的问题:有没有什么其他办法能够保证不退出进程(相关搜索:matlab调用)
  • ¥15 依据报错在原代吗格式的基础上解决问题