Skip to content

Instantly share code, notes, and snippets.

@gstricklind
Last active March 9, 2019 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gstricklind/f91e14732ff425bcef45b064b716d39b to your computer and use it in GitHub Desktop.
Save gstricklind/f91e14732ff425bcef45b064b716d39b to your computer and use it in GitHub Desktop.
Product Reviews Shortcode - only works on single-product template
/*========================================================================
3 Column Reviews shortcode - can only be used on single product pages
https://ginastricklind.com/how-to-create-a-product-reviews-shortcode-query/
========================================================================*/
add_shortcode( 'display_latest_reviews', 'gs_display_latest_reviews' );
function gs_display_latest_reviews() {
if( is_product() ) {
global $product;
$id = $product->id;
$args = array (
'post_type' => 'product',
'post_id' => $id,
'orderby' => 'commment_date',
'order' => DESC,
'number' => '3',
);
$comments = get_comments( $args );
ob_start();
?>
<div id="gs-reviews">
<ul class="review-list">
<?php $list = wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments); ?>
</ul>
</div>
<?
$output = ob_get_contents();
ob_get_clean();
return $output;
} else {
return 'shortcode must be used on a single product view';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment