Woocommerce的最新评论


Woocommerce latest review

我试图在我的测试页面上获得任何产品的最新评论/评论,到目前为止,我将其视为帖子,并得到这样的东西:

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

试试这个:

$number_of_reviews = 10; //How many reviews you want to retrieve
$reviews = get_comments( array( 'number' => $number_of_reviews, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product' ) );
echo "<ul>";
foreach ( (array) $reviews as $review ) {
    $_product = WC_get_product( $review->comment_post_ID );
    $rating = intval( get_comment_meta( $review->comment_ID, 'rating', true ) );
    echo '<li style="list-style:none"><a href="' . esc_url( get_comment_link( $review->comment_ID ) ) . '">';
        echo $_product->get_title() . '</a>  ';
        for($i=0;$i<$rating;$i++){
            echo "<span style='color:grey'>&#9733</span>";
        }
        echo "<p>".$review->comment_content."    -   ".get_comment_author($review->comment_ID)."</p>";
    echo '</li><br><br>';
}
echo "</ul>";

注意:你可以随时修改你的代码,根据你的需要和CSS以及

我是这样获取产品的最新评论的:

function display_product_review() {
    global $product;
    $comments = get_approved_comments( $product->id );
    $product_link = '/product/' . $product->post->post_name . "/#tab-reviews/";
    if ( !empty ($comments) ) {
        echo $comments[0]->comment_content . '<br><a href="'. $product_link . '">Read more reviews...</a>';
    } else {
        echo "There are no reviews for this product yet. Would you like to <a href='" . $product_link ."'>add your own review</a>?";       
    }
}

唯一的缺点是get_approved_comments只有一个参数,即post_id,因此它会拉出针对该产品的所有评论。这可能不是最干净的解决方案,但它确实适用于我。