Juan
Juan

Reputation: 31

How can I get 5 last reviews of each product in Woocommerce?

I am customizing a product page in Woocommerce and I would like to show 5 last reviews, and then a button with all reviews of the product and the possibility to add a review.

I have this code to put all comments/reviews, but I just want to show 5:

<?php
    $args = array ('post_type' => 'product');
    $comments = get_comments( $args );
    wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
?>

I know the existence of woocommerce_output_product_data_tabs(); hook, which I can use it to show everything.

Upvotes: 3

Views: 660

Answers (1)

Bhautik
Bhautik

Reputation: 11282

Try the below code.

<?php
    $args = array ('post_type' => 'product');
    $comments = get_comments( $args );
    wp_list_comments( array( 
        'callback' => 'woocommerce_comments',
        'per_page' => 5,
        'orderby' => 'comment_date_gmt',
        'order'   => 'DESC'
    ), $comments);
?>

Upvotes: 2

Related Questions