WooCommerce:在主页上随机显示一些评论 [英] WooCommerce: Display some reviews randomly on home page

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

问题描述

我希望每次有人访问时,我的主页上都会出现 5 条随机评论.

I want to have 5 random reviews appear on my home page each time someone visits.

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

I found some code to fetch all the reviews:

//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] 添加到这个 测试页面.

And it works just fine when I add the short code [woo_reviews] to this test page.

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

How do I change this to get only 5 random reviews?

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

Also How would I format this page now to make it only 5 reviews and be able to change the appearance of the reviews on the page (spacing, font etc)?

推荐答案

使用评论WP_Comment_Query评论不能随意排列.所以你需要使用一个简单的轻量级 SQL 查询,使用专用的 WordPressWPDB 类.

With the comment WP_Comment_Query, comments can't be in random order. so you need to use a simple lightweight SQL query instead, using dedicated WordPressWPDB Class.

在以下代码中,您可以更改样式和 html 结构以获得所需的输出.您还可以使用可用的简码参数 "limit" (默认设置为 5) 设置要以随机顺序显示的评论数量:

In the following code, you can change the styles and the html structure to get the desired output. You can also set the number of reviews you want to display in random order using available shortcode argument "limit" (default is set to 5):

add_shortcode('woo_reviews', 'get_random_woo_reviews');

function get_random_woo_reviews( $atts ){
    // Shortcode Attributes
    $atts = shortcode_atts( array(
        'limit' => '5', // <== Set to 5 reviews by default
    ), $atts, 'woo_reviews' );

    global $wpdb;

    // The SQL random query on product reviews
    $comments = $wpdb->get_results( $wpdb->prepare("
        SELECT *
        FROM  {$wpdb->prefix}comments c
        INNER JOIN {$wpdb->prefix}posts p ON c.comment_post_ID = p.ID
        WHERE c.comment_type = 'review' AND p.post_status = 'publish'
        ORDER BY RAND() LIMIT %d
    ", intval( esc_attr($atts['limit']) ) ) );

    ob_start(); // Start buffering

    ## CSS applied styles
    ?>
    <style>
        ul.product-reviews, ul.product-reviews li { list-style: none; margin:0; padding:0; line-height: normal;}
        ul.product-reviews li { display:block; max-width: 200px, padding: 10px; display:inline-block; vertical-align: text-top;}
        ul.product-reviews li .title {font-size: 1.2em;}
        ul.product-reviews li .content {max-width: 180px; font-size: 0.9em; margin-bottom: 6px;}
        ul.product-reviews li .author, ul.product-reviews li .date  {display: block; font-size: 0.75em;}
    </style>
    <?php

    ## HTML structure
    ?>
    <ul class="product-reviews"><?php

    foreach ( $comments as $comment ) {
        ?>
        <li>
            <h4 class="title"><?php echo get_the_title( $comment->comment_post_ID ); ?></h4>
            <div class="content"><?php echo $comment->comment_content; ?></div>
            <span class="author"><?php printf( __("Posted By %s") . ' ', '<strong>' . $comment->comment_author . '</strong>' ); ?></span>
            <span class="date"><?php printf( __("On %s"), '<strong>' . date_i18n( 'l jS \of F Y', strtotime( $comment->comment_date) ) . '</strong>' ); ?></span>

        </li>
        <?php
    }
    ?></ul><?php

    return ob_get_clean(); // Return the buffered output
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

用法: [woo_reviews] 或在 php 中:echo do_shortcode( "[woo_reviews]" );

USAGE: [woo_reviews] or in php: echo do_shortcode( "[woo_reviews]" );

这篇关于WooCommerce:在主页上随机显示一些评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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