php - 如何从 php 循环中自动生成 Schema.org Google 丰富片段
问题描述
我正在尝试在推荐中编辑 PHP 代码循环。我希望循环从 Schema.org 库中呈现“评论”丰富的片段。
<div class="testimonials">
<?php $loop = new WP_Query( array ('post_type' => 'testimonials', 'order' => 'DESC', 'orderby' => 'date', 'showposts' => '99' )); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="testimonial">
<?php if (has_post_thumbnail(get_the_ID())) { ?>
<div class="col-md-3 col-sm-4">
<img src="<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'testimonials_main' ); $url = $thumb['0']; echo $url; ?>" alt="<?php echo the_title(); ?>" />
</div>
<?php } ?>
<div class="col-md-9 col-sm-8">
<div class="testdesc"><?php echo the_content(); ?></div>
<div class="testname"><?php echo the_title(); ?> - <?php echo the_date(); ?></div>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
我想使用上面的代码从 Schema.org 创建多个 Rich Snippets,我希望该代码段看起来像这样:-
<script type='application/ld+json'>
{
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": "Wedding Hair & Makeup",
"reviewBody": "Hi girls, I was hoping you could pass on a huge thank you to Charmaine “Miss Blue Lily” for my wedding hair and makeup and my Friday night hair. It was completely perfect and I loved how I looked. Charmaine was so accommodating when I was changing my mind and a
delight to have around on the wedding day, a real credit to the Gemma
Sutton team So thank you so much! lots of love, Charlotte xx",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "5",
"worstRating": "0"
},
"author": "Charlotte",
"alternateName": "Wedding Hair & Makeup"
}
</script>
我认为我的代码应该看起来像这样,但我遇到了问题
<div class="testimonials">
<?php $loop = new WP_Query( array ('post_type' => 'testimonials', 'order' => 'DESC', 'orderby' => 'date', 'showposts' => '99' )); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="testimonial">
<?php if (has_post_thumbnail(get_the_ID())) { ?>
<div class="col-md-3 col-sm-4">
<img src="<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'testimonials_main' ); $url = $thumb['0']; echo $url; ?>" alt="<?php echo the_title(); ?>" />
</div>
<?php } ?>
<div class="col-md-9 col-sm-8">
<div class="testdesc"><?php echo the_content(); ?></div>
<div class="testname"><?php echo the_title(); ?> - <?php echo the_date(); ?></div>
</div>
<script type='application/ld+json'>
{
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": "Wedding Hair & Makeup",
"reviewBody": "<?php echo the_content(); ?>",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "5",
"worstRating": "0"
},
"author": "<?php echo the_title(); ?>",
"alternateName": "Wedding Hair & Makeup"
}
</script>
</div>
<?php endwhile; wp_reset_postdata(); ?>
上面的代码没有从数据库中提取正确的值。我不确定我是否应该设置一个 var ?
解决方案
推荐阅读
- javascript - firebase.firestore.FieldValue.serverTimestamp() as int
- bash - 在 Bash 中,cd ./someDir 和 cd someDir 之间有什么实际区别吗?
- python - matplotlib 回调函数在 Visual Studio Code 中不起作用
- c++ - 使用 createDefaultAxes() 在 QT 中只显示一个小的 X 轴和一个小的 Y 轴,值为 1
- pyspark - 如何使用 map 函数正确并行运行 pyspark 代码
- swagger-ui - 嵌套参考在 Swagger UI / OpenAPI 3 中不起作用
- java - 辅助功能服务:设置 webview EditText 字段的光标
- android - 我们如何在android / ios react native中为键盘提供自定义颜色
- react-native - 如何为 React Navigation TabBar 创建渐变边框颜色
- junit - Junit黄瓜重试失败场景:我使用的mvn命令:“test -e -X -U”-Dcucumber.options=--tags ${tag1}”。它没有调用重试场景