首页 > 解决方案 > 如何从 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 ?

标签: phpwordpressschema.orggoogle-rich-snippets

解决方案


推荐阅读