Микроразметка JSON-LD. Пример на PHP для WordPress

Документация по микроразметке https://schema.org/Product
Валидация микроразметки https://search.google.com/structured-data/testing-tool/u/0/?hl=ru

	
<?php if( is_product() ) : 
    global $post, $product;
    $date = strtotime( $post->post_date );
    $date = strtotime('+1 year', $date);
    $reviews = get_comments(array(
                        'post_id'       => $post->ID,
			'status'        => 'approve'
                    ));
    ?>
<script type="application/ld+json">
	{
		"@context": "http://schema.org/",
		"@type": "Product",
		"name": "<?= $post->post_title; ?>",
		"description": "<?= $post->post_title; ?>",
		<?php if( ( $comments = get_comments_number( $product->get_id() ) ) != 0 ) : ?>
		"aggregateRating": {
			"@type": "AggregateRating",
			"ratingValue": "<?= getProductRating($post->ID); ?>",
			"bestRating": "5",
			"reviewCount": "<?= $comments ?>"
		},
		"review": [
        <?php foreach($reviews as $key => $review):?>
        <?php reset($reviews);?>
        <?php $rating = get_comment_meta($review->comment_ID, 'rating', true);?>
        {
          "@type": "Review",
          "author": "<?= $review->comment_author; ?>",
          "datePublished": "<?= date('Y-m-d', strtotime($review->comment_date)); ?>",
          "description": "<?= htmlspecialchars($review->comment_content); ?>",
          "name": "Отзыв",
          "reviewRating": {
            "@type": "Rating",
            "bestRating": "5",
            "ratingValue": "<?= empty($rating)|| $rating == 0 ? 5 : intval($rating);?>",
            "worstRating": "1"
          }
        }
        <?php end($reviews);  if ($key !== key($reviews)) echo(",");?>
        <?php endforeach;?>
        ],
		<?php endif; ?> 
		"sku": "<?= $product->get_sku(); ?>",
		"brand": "MoxieMaks",
		"url": "<?= get_the_permalink($post); ?>",
		"image": "<?= array_shift(wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "full")) ?>",
		"offers": {
			"@type": "Offer",
			"priceCurrency": "RUB",
			"url": "<?= get_the_permalink($post); ?>",
			"price": "<?= $product->get_price(); ?>",
			"priceValidUntil": "<?= date('Y-m-d\TH:i:sO', $date ); ?>",
			"itemCondition": "http://schema.org/UsedCondition",
			<?php if( $product->get_stock_status() == 'instock' ) : ?> "availability": "http://schema.org/InStock",
			<?php else : ?> "availability": "http://schema.org/OutOfStock",
			<?php endif; ?> "seller": {
				"@type": "Organization",
				"name": "MoxieMaks"
			}
		}
	}
</script>
<?php endif; ?>

Добавить комментарий