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

如何在JSON-LD中包含WordPress PHP标记?,php,wordpress,woocommerce,json-ld,google-rich-snippets,Php,Wordpress,Woocommerce,Json Ld,Google Rich Snippets,我想将JSON-LD结构化数据添加到我的WordPress WooCommerce网站,以增加我在搜索引擎结果页面上出现丰富片段的机会 我想首先将其添加到我的产品页面 如果我在header shop.php中包含了下面的代码 <!-- Include Schema Markup File –––––––––––––––––––––––––––––––––––––––––––––––––– --> <?php include('json-ld.php'); ?>&l

我想将JSON-LD结构化数据添加到我的WordPress WooCommerce网站,以增加我在搜索引擎结果页面上出现丰富片段的机会

我想首先将其添加到我的产品页面

如果我在header shop.php中包含了下面的代码

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

如何在json-ld.PHP文件中引用PHP标记,如

它是否像下面这样简单

<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>

{
“@context”:”http://schema.org/",
“@type”:“产品”,
“名称”:“,
“图像”:[
"https://example.com/photos/1x1/photo.jpg",
"https://example.com/photos/4x3/photo.jpg",
"https://example.com/photos/16x9/photo.jpg"
],
“品牌”:{
“@type”:“Thing”,
“名称”:“ACME”
},
“聚合”:{
“@type”:“聚合”,
“额定值”:“4.4”,
“评级计数”:“89”
},
“提议”:{
“@type”:“AggregateOffer”,
“低价”:“119.99”,
“高价”:“199.99”,
“价格货币”:“美元”
}
}

您可以在PHP中执行此操作以打印到页面。通过完整地回显您的脚本,将允许您生成内联函数

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>
';
echo'
{
“@context”:”http://schema.org/",
“@type”:“产品”,
“名称”:“.”。标题(),
“图像”:[
"https://example.com/photos/1x1/photo.jpg",
"https://example.com/photos/4x3/photo.jpg",
"https://example.com/photos/16x9/photo.jpg"
],
“品牌”:{
“@type”:“Thing”,
“名称”:“ACME”
},
“聚合”:{
“@type”:“聚合”,
“额定值”:“4.4”,
“评级计数”:“89”
},
“提议”:{
“@type”:“AggregateOffer”,
“低价”:“119.99”,
“高价”:“199.99”,
“价格货币”:“美元”
}
}
';

对于文章,以下是我使用的内容(我想将其应用于产品不会太复杂):


建议在head中包含ld-json,然后再包含所有其他元标记,如title/description等。

我建议您使用所需的所有数据创建一个PHP数组/对象。然后,使用
json\u encode
呈现缩小的json。另外,使用
wp_头
hook。关于WP标签,所有的
标签都有一个
get\u版本。检查WP函数,您将看到调用了什么函数。感谢@Maxwells.c的响应!我将把PHP数组放在哪里?在json-ld.php中?在javascript标记中?还是在他们之外?或者我甚至不需要javascript标记/脚本?如何确保输出正确呈现为JSON-LD?我到底需要什么?标签?和/或我是否需要使用
获取函数与标记?是否有任何方法可以包含一些您建议的代码示例?许多问题。首先,我建议您查看JSON-LD插件()并检查它是否解决了您的问题。关于
获取函数,当您不想输出数据时使用它们,只需获取数据即可。检查插件后,我可以解释你想要什么。这非常简单!非常感谢@proeviz!
    function add_ld_json()
    {
        // get post thumbnail
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'post-full');
    
        // get canonical
        $post_full_url = wp_get_canonical_url();
    
        // get title
        $page_h1 = the_title('', '', false);
    
        // create ld-json with structured data
        $ld_json = <<< EOT
    
        <script type="application/ld+json">
        {
            "@context": "https://schema.org/",
            "@type": "TechArticle",
            "mainEntityOfPage": {
                "@type": "WebPage",
                "@id": "{$post_full_url}"
            },
            "headline": "{$page_h1}",
            "image": "{$thumb[0]}",
            "author": {
                "@type": "Organization",
                "name": "your-organization-name"
            }
        }
        </script>
    EOT;
    
        echo "{$ld_json}";
    }
if (!is_front_page() and !is_404())
add_action('wp_head', 'add_ld_json', -1);