网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

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

GG网络技术分享 2025-03-18 16:12 3


问题描述:

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>

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我想将JSON-LD结构化数据添加到我的WordPress WooCommerce网站,以增加我的丰富网页摘要的机会 搜索引擎结果页面。 </ p>

我想首先将它添加到我的产品页面。 </ p>

如果我在header-shop.php中包含以下代码... </ p>

 &lt;! - 包含架构标记 档案

----------------------------------------------- --- - &gt;

&lt;?php include(\'json-ld.php\'); ?&gt;&lt; script type =“application / ld + json”&gt;&lt;?php echo json_encode($ payload); ?&gt;&lt; / script&gt;

</ code> </ pre>

如何引用PHP代码,如&lt; php the_title(); json-ld.php文件中的?&gt; </ code>?</ p>

它是否像以下一样简单?</ p>

 &lt  ; script type =“application / ld + json”&gt; 

{

“@context”:“http://schema.org/\",

”@type“:”Product“,

”name“ :“&lt;?php the_title();?&gt;”,

“image”:[

“https://example.com/photos/1x1/photo.jpg\",

”https:// example .com / photos / 4x3 / photo.jpg“,

”https://example.com/photos/16x9/photo.jpg\"

],

“品牌”:{

“@type”:“ 事情“,

”名称“:”ACME“

},

”“AggRating”:{

“@type”:“AggregateRating”,

“ratingValue”:“4.4”,

“ratingCount”: “89”

},

“提供”:{

“@type”:“AggregateOffer”,

“lowPrice”:“119.99”,

“highPrice”:“199.99”,

“priceCurrency” :“USD”

}

}

&lt; / script&gt;

</ code> </ pre>

</ div>

网友观点:

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>

\';

标签:

提交需求或反馈

Demand feedback