Php WooCommerce:随机显示一些5星级产品评论

Php WooCommerce:随机显示一些5星级产品评论,php,wordpress,woocommerce,product,shortcode,Php,Wordpress,Woocommerce,Product,Shortcode,根据答案代码,我使用以下代码显示5个随机评论: function get_woo_reviews() { $comments = get_comments( array( 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product', ) ); s

根据答案代码,我使用以下代码显示5个随机评论:

function get_woo_reviews()
{
    $comments = get_comments(
        array(
            'status'      => 'approve',
            'post_status' => 'publish',
            'post_type'   => 'product',
        )
    ); 
    shuffle($comments);
    $comments = array_slice( $comments, 0, 5 );
    $html = '<ul>';
    foreach( $comments as $comment ) :
        $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
        $html .= '<li class="review">';
        $html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';
        if ( $rating > 4 ) $html .= wc_get_rating_html( $rating );
        $html .= '<div>' .$comment->comment_content.'</div>';
        $html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
        $html .= '</li>';
    endforeach;
    $html .= '</ul>';
    ob_start();
    echo $html;
        $html = ob_get_contents();
    ob_end_clean();

    return $html;
}
add_shortcode('woo_reviews', 'get_woo_reviews');
函数get\u woo\u reviews()
{
$comments=获取注释(
排列(
“状态”=>“批准”,
“发布状态”=>“发布”,
“post_类型”=>“产品”,
)
); 
洗牌(评论);
$comments=array\u slice($comments,0,5);
$html=“
    ”; foreach($comments作为$comment): $rating=intval(获取注释元($comment->comment_ID,'rating',true)); $html.='
  • ; $html.=''。获取标题($comment->comment\u post\u ID)。''; 如果($rating>4)$html.=wc\u get\u rating\u html($rating); $html.=''.$comment->comment_content'; $html.=“发布人:“$comment->comment\u author.”在“$comment->comment\u date.”上; $html.='
  • '; endforeach; $html.='
'; ob_start(); echo$html; $html=ob_get_contents(); ob_end_clean(); 返回$html; } 添加快捷码(“woo_评论”、“get_woo_评论”);
但它也显示了一些没有任何评级的评论

如何更改此代码以仅显示五星评级评论?

基于“评级”元键设置为5星元值,需要对
WP\u评论\u查询进行“元查询”,以获得具有五星评级的随机评论,如下所示:

function get_random_five_stars_products_reviews( $atts ) {
    // Extract shortcode attributes
    extract( shortcode_atts( array(
        'limit' => 5, // number of reviews to be displayed by default
    ), $atts, 'woo_reviews' ) );

    $comments = get_comments( array(
        'status'      => 'approve',
        'post_status' => 'publish',
        'post_type'   => 'product',
        'meta_query'  => array( array(
            'key'     => 'rating',
            'value'   => '5',
        ) ),
    ) );

    shuffle($comments);

    $comments = array_slice( $comments, 0, $limit );

    $html = '<ul class="products-reviews">';
    foreach( $comments as $comment ) {
        $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
        $html .= '<li class="review">';
        $html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';
        if ( $rating > 4 ) $html .= wc_get_rating_html( $rating );
        $html .= '<div>' .$comment->comment_content.'</div>';
        $html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
        $html .= '</li>';
    }
    return $html . '</ul>';
}
add_shortcode('woo_reviews', 'get_random_five_stars_products_reviews');
函数获取\随机\五星\产品\评论($atts){
//提取短代码属性
提取(短码)附件(数组)(
'limit'=>5,//默认情况下显示的评论数
),$atts,'woo_reviews');
$comments=获取注释(数组)(
“状态”=>“批准”,
“发布状态”=>“发布”,
“post_类型”=>“产品”,
“元查询”=>数组(数组(
“键”=>“评级”,
'值'=>'5',
) ),
) );
洗牌(评论);
$comments=array\u slice($comments,0,$limit);
$html='
    '; foreach($comments作为$comment){ $rating=intval(获取注释元($comment->comment_ID,'rating',true)); $html.='
  • ; $html.=''。获取标题($comment->comment\u post\u ID)。''; 如果($rating>4)$html.=wc\u get\u rating\u html($rating); $html.=''.$comment->comment_content'; $html.=“发布人:“$comment->comment\u author.”在“$comment->comment\u date.”上; $html.='
  • '; } 返回$html。“
”; } 添加快捷码(“woo_评论”、“随机获取五星级产品评论”);
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作


用法
[woo_reviews]
[woo_reviews limit=“3”]
用于3次随机评论。

谢谢。工作完美。如何帮助:如何将日期格式转换为MJY格式?@Polmario将
$comment->comment\u date
替换为
日期('MJY',strotime($comment->comment\u date))
。谢谢。所有的工作都很完美,最终的调整。我们可以选择最长的评论吗?我的一些评论有7-10句话。我们可以限制符号数吗(比如最多100个字母)?完成