WooCommerce:在主页上随机显示一些评论

我希望每次有人访问时在我的主页上显示 5 条随机评论。


我找到了一些代码来获取所有评论:


//add get product reviews to homepage


function get_woo_reviews()

{

    $count = 0;

    $html_r = "";

    $title="";

    $args = array(

        'post_type' => 'product'

    );


    $comments_query = new WP_Comment_Query;

    $comments = $comments_query->query( $args );


    foreach($comments as $comment) :

        $title = ''.get_the_title( $comment->comment_post_ID ).'';

        $html_r = $html_r. "" .$title."";

        $html_r = $html_r. "" .$comment->comment_content."";

        $html_r = $html_r."Posted By".$comment->comment_author." On ".$comment->comment_date. "";

    endforeach;


    return $html_r;

}


add_shortcode('woo_reviews', 'get_woo_reviews');

当我将短代码添加[woo_reviews]到此测试页面时,它工作得很好。


如何更改此设置以仅获得 5 条随机评论?


另外,我现在如何格式化此页面以使其仅包含 5 条评论并能够更改页面上评论的外观(间距、字体等)?


繁花如伊
浏览 238回答 2
2回答

喵喔喔

添加以下代码片段 -function get_woo_reviews(){&nbsp; &nbsp; $comments = get_comments(&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'status'&nbsp; &nbsp; &nbsp; => 'approve',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'post_status' => 'publish',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'post_type'&nbsp; &nbsp;=> 'product',&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; );&nbsp;&nbsp; &nbsp; shuffle($comments);&nbsp; &nbsp; $comments = array_slice( $comments, 0, 5 );&nbsp; &nbsp; $html = '<ul>';&nbsp; &nbsp; foreach( $comments as $comment ) :&nbsp; &nbsp; &nbsp; &nbsp; $html .= '<li><h2>'.get_the_title( $comment->comment_post_ID ).'</h2>';&nbsp; &nbsp; &nbsp; &nbsp; $html .= '<p>' .$comment->comment_content.'</p>';&nbsp; &nbsp; &nbsp; &nbsp; $html .= "<p>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</p></li>";&nbsp; &nbsp; endforeach;&nbsp; &nbsp; $html .= '</ul>';&nbsp; &nbsp; ob_start();&nbsp; &nbsp; echo $html;&nbsp; &nbsp; &nbsp; &nbsp; $html = ob_get_contents();&nbsp; &nbsp; ob_end_clean();&nbsp; &nbsp; return $html;}add_shortcode('woo_reviews', 'get_woo_reviews');

呼如林

有了评论WP_Comment_Query,评论不能乱序。所以你需要使用一个简单的轻量级 SQL 查询,使用专用的 WordPressWPDB类。在以下代码中,您可以更改样式和 html 结构以获得所需的输出。您还可以使用可用的短代码参数“limit ” (默认设置为5)设置要以随机顺序显示的评论数量:add_shortcode('woo_reviews', 'get_random_woo_reviews');function get_random_woo_reviews( $atts ){&nbsp; &nbsp; // Shortcode Attributes&nbsp; &nbsp; $atts = shortcode_atts( array(&nbsp; &nbsp; &nbsp; &nbsp; 'limit' => '5', // <== Set to 5 reviews by default&nbsp; &nbsp; ), $atts, 'woo_reviews' );&nbsp; &nbsp; global $wpdb;&nbsp; &nbsp; // The SQL random query on product reviews&nbsp; &nbsp; $comments = $wpdb->get_results( $wpdb->prepare("&nbsp; &nbsp; &nbsp; &nbsp; SELECT *&nbsp; &nbsp; &nbsp; &nbsp; FROM&nbsp; {$wpdb->prefix}comments c&nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN {$wpdb->prefix}posts p ON c.comment_post_ID = p.ID&nbsp; &nbsp; &nbsp; &nbsp; WHERE c.comment_type = 'review' AND p.post_status = 'publish'&nbsp; &nbsp; &nbsp; &nbsp; ORDER BY RAND() LIMIT %d&nbsp; &nbsp; ", intval( esc_attr($atts['limit']) ) ) );&nbsp; &nbsp; ob_start(); // Start buffering&nbsp; &nbsp; ## CSS applied styles&nbsp; &nbsp; ?>&nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; ul.product-reviews, ul.product-reviews li { list-style: none; margin:0; padding:0; line-height: normal;}&nbsp; &nbsp; &nbsp; &nbsp; ul.product-reviews li { display:block; max-width: 200px, padding: 10px; display:inline-block; vertical-align: text-top;}&nbsp; &nbsp; &nbsp; &nbsp; ul.product-reviews li .title {font-size: 1.2em;}&nbsp; &nbsp; &nbsp; &nbsp; ul.product-reviews li .content {max-width: 180px; font-size: 0.9em; margin-bottom: 6px;}&nbsp; &nbsp; &nbsp; &nbsp; ul.product-reviews li .author, ul.product-reviews li .date&nbsp; {display: block; font-size: 0.75em;}&nbsp; &nbsp; </style>&nbsp; &nbsp; <?php&nbsp; &nbsp; ## HTML structure&nbsp; &nbsp; ?>&nbsp; &nbsp; <ul class="product-reviews"><?php&nbsp; &nbsp; foreach ( $comments as $comment ) {&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h4 class="title"><?php echo get_the_title( $comment->comment_post_ID ); ?></h4>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="content"><?php echo $comment->comment_content; ?></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="author"><?php printf( __("Posted By %s") . ' ', '<strong>' . $comment->comment_author . '</strong>' ); ?></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="date"><?php printf( __("On %s"), '<strong>' . date_i18n( 'l jS \of F Y', strtotime( $comment->comment_date) ) . '</strong>' ); ?></span>&nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; }&nbsp; &nbsp; ?></ul><?php&nbsp; &nbsp; return ob_get_clean(); // Return the buffered output}代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。用法: [woo_reviews]或在 php 中:echo do_shortcode( "[woo_reviews]" );
打开App,查看更多内容
随时随地看视频慕课网APP