Skip to content

Instantly share code, notes, and snippets.

@icreatesolutions
Created September 29, 2016 04:16
Show Gist options
  • Save icreatesolutions/433bfe8d9c093a3cef050daee36988aa to your computer and use it in GitHub Desktop.
Save icreatesolutions/433bfe8d9c093a3cef050daee36988aa to your computer and use it in GitHub Desktop.
Aggregate Reviews Schema Tags
<?php
function display_aggregate_rating() {
$num_ratings = 0;
$agg_score = 0;
$args = array(
'post_type' => 'testimonials',
'posts_per_page' => '-1'
);
$testimonials = get_posts( $args );
foreach ( $testimonials as $testimonial ) {
$rating = get_post_meta( $testimonial->ID, 'rating', true );
if ( $rating ) {
$num_ratings = $num_ratings + 1;
$agg_score = $agg_score + $rating;
}
}
$average_score = $agg_score / $num_ratings;
?>
<div class="rating" itemscope="" itemtype="http://schema.org/Product">
<span itemprop="name">Product Name</span>
<span itemprop="aggregateRating" itemscope="" itemtype="http://schema.org/AggregateRating">
Rated
<span itemprop="ratingValue"><?php echo $average_score; ?></span>
/5 based on
<span itemprop="reviewCount"><?php echo $num_ratings; ?></span>
customer reviews
</span>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment