随机显示一些内容长度有限的 WooCommerce 产品评论 [英] Display some WooCommerce product reviews with limited content length randomly

查看:61
本文介绍了随机显示一些内容长度有限的 WooCommerce 产品评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据 WooCommerce 制作评论的简码: 在主页上随机显示一些评论放在其他页面上.如何按字符数限制评论,例如 100 或 200 个字符.所以它们不会太长并且适合任何页面.

我现在正在使用以下代码进行评论

function get_random_five_stars_products_reviews( $atts ) {//提取简码属性提取(shortcode_atts(数组('限制' =>5,//默认要显示的评论数), $atts, 'woo_reviews' ) );$comments = get_comments( 数组('状态' =>'批准','post_status' =>'发布','post_type' =>'产品','meta_query' =>数组(数组('键' =>'评分','价值' =>'5',) ),) );洗牌($评论);$comments = array_slice( $comments, 0, $limit );$html = '
    ';foreach( $comments 作为 $comment ) {$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );$html .='
  • ';$html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';if ( $rating > 4 ) $html .= wc_get_rating_html( $rating );$html .='

    '.$comment->comment_content.'

    ';$html .= "<div>发布者:".$comment->comment_author."在".$comment->comment_date.</div>";$html .='

  • ';}返回 $html .'</ul>';}add_shortcode('woo_reviews', 'get_random_five_stars_products_reviews');

解决方案

要限制显示的 5 个开始评论的内容不超过特定长度(此处为 100 个字符),请使用以下内容:

function get_random_five_stars_products_reviews( $atts ) {//提取简码属性提取(shortcode_atts(数组('限制' =>5,//默认要显示的评论数'长度' =>100,//限制评论文本内容长度'show_stars' =>true,//或假"隐藏), $atts, 'woo_reviews' ) );$comments = get_comments( 数组('状态' =>'批准','post_status' =>'发布','post_type' =>'产品','meta_query' =>数组(数组('键' =>'评分','价值' =>'5',) ),) );洗牌($评论);$count = 0;//初始化$html = '
    ';foreach( $comments 作为 $comment ) {$content = $comment->comment_content;if( strlen($content) <= $length && $count < $limit ) {$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );$html .='
  • ';$html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';如果($show_stars){$html .= wc_get_rating_html( $rating );}$html .='

    '.$内容.'</div>';$html .= "<div>发布者:".$comment->comment_author."在".$comment->comment_date.</div>";$html .='
  • ';$count++;//增加计数}如果 ( $count == $limit ) {休息;//停止循环}}返回 $html .'</ul>';}add_shortcode('woo_reviews', 'get_random_five_stars_products_reviews');

代码位于活动子主题(或活动主题)的functions.php 文件中.它应该有效.

I am making shortcode for reviews Based on WooCommerce: Display some reviews randomly on home page to put on other pages. How can i limit reviews by character numbers, like 100 or 200 characters. SO they will not be too long and fit any page.

I am using following code for reviews now

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');

解决方案

To limit displayed 5 starts reviews with a content that doesn't exceed a specific length (here 100 characters) use the following:

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
        'length'     => 100, // Limit comments text content lenght
        'show_stars' => true, // Or "false" to hide
    ), $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);

    $count = 0; // Initializing
    $html  = '<ul class="products-reviews">';
    
    foreach( $comments as $comment ) {
        $content = $comment->comment_content;
        if( strlen($content) <= $length && $count < $limit ) {
            $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 ( $show_stars ) {
                $html .= wc_get_rating_html( $rating );
            }
            $html .= '<div>' .$content. '</div>';
            $html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
            $html .= '</li>';
            $count++; // Increase count
        }

        if ( $count == $limit ) {
            break; // Stop the loop
        }
    }
    return $html . '</ul>';
}
add_shortcode('woo_reviews', 'get_random_five_stars_products_reviews');

Code goes in functions.php file of the active child theme (or active theme). It should works.

这篇关于随机显示一些内容长度有限的 WooCommerce 产品评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆