Woocommerce 建立所有提交的商业评论的列表
我试图通过使用查询创建所有已批准的WooCommerce评论的列表,并尝试使用快捷码在页面上显示该列表 其要点是显示客户姓名、电子邮件和提交审核的日期。以下是代码和我迄今为止尝试的内容:Woocommerce 建立所有提交的商业评论的列表,woocommerce,Woocommerce,我试图通过使用查询创建所有已批准的WooCommerce评论的列表,并尝试使用快捷码在页面上显示该列表 其要点是显示客户姓名、电子邮件和提交审核的日期。以下是代码和我迄今为止尝试的内容: function list_reviews() { $customer_details_from_reviews = $wpdb->get_results("SELECT wpc.comment_author,wpc.comment_author_email,wpc.comment_date,
function list_reviews() {
$customer_details_from_reviews = $wpdb->get_results("SELECT wpc.comment_author,wpc.comment_author_email,wpc.comment_date,wpc.comment_content,wpcm.meta_value AS rating FROM `" . $wpdb->prefix . "comments` AS wpc INNER JOIN `" . $wpdb->prefix . "commentmeta` AS wpcm ON wpcm.comment_id = wpc.comment_id AND wpcm.meta_key = 'rating' WHERE wpc.comment_post_id = '" . $p_id . "' ");
}
add_shortcode('allreviews', 'list_reviews');
使用
[allreviews]
时,屏幕上不会显示任何内容。如果有人能帮上忙,我将不胜感激。首先,在您的功能中,您只需查询数据库,而无需循环查询结果并打印它们,第二,Wordpress中已经内置了一个函数,它可以帮助您从数据库中获取注释,而无需编写自定义查询,如果可能的话,这是遵循Wordpress标准的明智选择
因此,要使用get_comments()
函数获取注释,您的代码应该如下所示:
function list_reviews()
{
$args = array(
'post_type' => 'product', //Post type
'status' => "approve", // Status you can also use 'hold', 'spam', 'trash',
);
$comments = get_comments($args);
foreach ($comments as $comment) {
echo "Customer Name " . $comment->comment_author . " Email: " . $comment->comment_author_email . " Date " . $comment->comment_date . "<br>";
}
}
add_shortcode('allreviews', 'list_reviews');
我猜是一个星期来一直在处理同一个问题,并设法找到了一个解决方案,可以在一个页面上输出所有的产品评论
//Display all reviews
if (!function_exists('display_all_reviews')) {
function display_all_reviews(){
$args = array(
'status' => 'approve',
'type' => 'review'
);
// The Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
// Comment Loop
if ( $comments ) {
echo "<ol>";
foreach ( $comments as $comment ): ?>
<?php if ( $comment->comment_approved == '0' ) : ?>
<p class="meta waiting-approval-info">
<em><?php _e( 'Thanks, your review is awaiting approval', 'woocommerce' ); ?></em>
</p>
<?php endif; ?>
<li itemprop="reviews" itemscope itemtype="http://schema.org/Review" <?php comment_class(); ?> id="li-review-<?php echo $comment->comment_ID; ?>">
<div id="review-<?php echo $comment->comment_ID; ?>" class="review_container">
<div class="review-avatar">
<?php echo get_avatar( $comment->comment_author_email, $size = '50' ); ?>
</div>
<div class="review-author">
<div class="review-author-name" itemprop="author"><?php echo $comment->comment_author; ?></div>
<div class='star-rating-container'>
<div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating" class="star-rating" title="<?php echo esc_attr( get_comment_meta( $comment->comment_ID, 'rating', true ) ); ?>">
<span style="width:<?php echo get_comment_meta( $comment->comment_ID, 'rating', true )*22; ?>px"><span itemprop="ratingValue"><?php echo get_comment_meta( $comment->comment_ID, 'rating', true ); ?></span> <?php _e('out of 5', 'woocommerce'); ?></span>
<?php
$timestamp = strtotime( $comment->comment_date ); //Changing comment time to timestamp
$date = date('F d, Y', $timestamp);
?>
</div>
<em class="review-date">
<time itemprop="datePublished" datetime="<?php echo $comment->comment_date; ?>"><?php echo $date; ?></time>
</em>
</div>
</div>
<div class="clear"></div>
<div class="review-text">
<div itemprop="description" class="description">
<?php echo $comment->comment_content; ?>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</li>
<?php
endforeach;
echo "</ol>";
} else {
echo "This product hasn't been rated yet.";
}
}
}
add_shortcode('allreviews', 'display_all_reviews');
//显示所有评论
如果(!function_存在('display_all_reviews')){
功能显示所有检查(){
$args=数组(
“状态”=>“批准”,
'类型'=>'审查'
);
//询问
$comments\u query=新的WP\u Comment\u查询;
$comments=$comments\u query->query($args);
//注释循环
如果($评论){
回声“;
foreach($comments作为$comment):?>
感谢您的帮助、解释和时间。非常感谢。我可以问@kashalo,如何将其转换为列吗?@Learning如何将其编码为表格或网格系统?作为表格(列)。这样,在将其复制/粘贴为Excel值后,我可以将其过滤。感谢您的帮助。太棒了!感谢您的所有帮助。衷心感谢。
//Display all reviews
if (!function_exists('display_all_reviews')) {
function display_all_reviews(){
$args = array(
'status' => 'approve',
'type' => 'review'
);
// The Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
// Comment Loop
if ( $comments ) {
echo "<ol>";
foreach ( $comments as $comment ): ?>
<?php if ( $comment->comment_approved == '0' ) : ?>
<p class="meta waiting-approval-info">
<em><?php _e( 'Thanks, your review is awaiting approval', 'woocommerce' ); ?></em>
</p>
<?php endif; ?>
<li itemprop="reviews" itemscope itemtype="http://schema.org/Review" <?php comment_class(); ?> id="li-review-<?php echo $comment->comment_ID; ?>">
<div id="review-<?php echo $comment->comment_ID; ?>" class="review_container">
<div class="review-avatar">
<?php echo get_avatar( $comment->comment_author_email, $size = '50' ); ?>
</div>
<div class="review-author">
<div class="review-author-name" itemprop="author"><?php echo $comment->comment_author; ?></div>
<div class='star-rating-container'>
<div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating" class="star-rating" title="<?php echo esc_attr( get_comment_meta( $comment->comment_ID, 'rating', true ) ); ?>">
<span style="width:<?php echo get_comment_meta( $comment->comment_ID, 'rating', true )*22; ?>px"><span itemprop="ratingValue"><?php echo get_comment_meta( $comment->comment_ID, 'rating', true ); ?></span> <?php _e('out of 5', 'woocommerce'); ?></span>
<?php
$timestamp = strtotime( $comment->comment_date ); //Changing comment time to timestamp
$date = date('F d, Y', $timestamp);
?>
</div>
<em class="review-date">
<time itemprop="datePublished" datetime="<?php echo $comment->comment_date; ?>"><?php echo $date; ?></time>
</em>
</div>
</div>
<div class="clear"></div>
<div class="review-text">
<div itemprop="description" class="description">
<?php echo $comment->comment_content; ?>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</li>
<?php
endforeach;
echo "</ol>";
} else {
echo "This product hasn't been rated yet.";
}
}
}
add_shortcode('allreviews', 'display_all_reviews');