woocommerce近期X评论的产品

时间:2015-12-23 20:33:49

标签: wordpress woocommerce

我正在寻找Woocommerce中的代码,允许我显示添加了最近评论(评论)的x项

simlar to [recent_products per_page =" 16"列=" 4" orderby =" rand"]短代码,但只允许发表评论

2 个答案:

答案 0 :(得分:0)

我不相信有一个短代码。你必须写一些PHP。你可以这样做的方式是:

    $number = 5; // Change as desired
    $comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product' ) );

    if ( $comments ) {
        echo '<ul class="product_list_widget">';
        foreach ( (array) $comments as $comment ) {
            $_product = wc_get_product( $comment->comment_post_ID );
            $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
            $rating_html = $_product->get_rating_html( $rating );
            echo '<li><a href="' . esc_url( get_comment_link( $comment->comment_ID ) ) . '">';
            echo $_product->get_image();
            echo $_product->get_title() . '</a>';
            echo $rating_html;
            printf( '<span class="reviewer">' . _x( 'by %1$s', 'by comment author', 'woocommerce' ) . '</span>', get_comment_author() );
            echo '</li>';
        }
        echo '</ul>';
    }

(注意:此代码通过WooCommerce插件的修改解除,&#34;最近的评论&#34;小部件:woocommerce/includes/widgets/class-wc-widget-recent-reviews.php

答案 1 :(得分:0)

这是未经测试的,但这样的事情应该有效:

    $args = array(
        'post_type' => 'product',
        'orderby' => 'comment_count',
        'order' => 'DESC'
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            if ( $post->comment_count < 1 ) continue;
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();