Gre*_*ory 5 wordpress wordpress-plugin woocommerce
我在wordpress网站上有一个产品供应商woocommerce设置.人们可以注册并添加自己的产品,这些产品只是自定义帖子类型等,购买这些产品的其他人可以查看它们.评论是WordPress自定义帖子类型产品的评论模板的一部分.我正在使用以下代码显示购买产品的人(自定义帖子类型)的单评(wordpress评论):
我想要工作的主要部分是通过woocommerce插件添加的星级评分:
echo('Rating: <div class="star-rating" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating"><span style="width:' . ( get_comment_meta( $comment->comment_ID, 'rating', true ) / 5 ) * 100 . '%"><strong itemprop="ratingValue">' . get_comment_meta( $comment->comment_ID, 'rating', true ) . '</strong></span></div><br />');
Run Code Online (Sandbox Code Playgroud)
完整代码:
<?php
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
$args = array(
'orderby' => 'date',
'post_type' => 'product',
'number' => '4',
'post_author' => $user_id
);
$comments = get_comments($args);
foreach($comments as $comment) :
echo '<div>';
echo('Review By: ' . $comment->comment_author . '<br />');
echo('Product: ' . '<a href="' . post_permalink($comment->ID)
. '">' . $comment->post_title . '</a>' . '<br />');
echo('Date: ' . $comment->comment_date . '<br />');
echo('Rating: <div class="star-rating" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating"><span style="width:' . ( get_comment_meta( $comment->comment_ID, 'rating', true ) / 5 ) * 100 . '%"><strong itemprop="ratingValue">' . get_comment_meta( $comment->comment_ID, 'rating', true ) . '</strong></span></div><br />');
echo('Review: ' . $comment->comment_content );
echo '</div>';
endforeach;
}
?>
Run Code Online (Sandbox Code Playgroud)
这段代码放在一个页面上,并使用它运行的插件Exec PHP.
我将它添加到我创建的另一个页面(常规wordpress页面)并从数组中删除'number'=> 4,以便显示所有评论(评论).这没用.所以我复制了代码,角色的角色,但这仍然无效.
所以它在"管理"页面上显示评级,这只是评论元,而不是在他们的"反馈"页面上.
现在我已经加载了网站,它已经停止在任一页面上显示评级.
任何人都可以帮忙解释一下吗?
global $wpdb;
$args = apply_filters('product_reviews_args', array(
'status' => 'all',
'orderby' => 'comment_ID',
'order' => 'ASC',
'post_type' => 'product',
));
$stars = apply_filters('product_reviews_ratings', array(3, 4)); // change the star rating needed here
if (!empty($stars)) {
$args['meta_query'] = array(array('key' => 'rating', 'value' => $stars));
}
$comment_query = new WP_Comment_Query;
$comments = $comment_query->query($args);
foreach ($comments as $comment) {
$rating = get_comment_meta($comment_ID, 'rating', true);
$verfied_customer = get_comment_meta($comment_ID, 'verified', true);
$review_title = get_comment_meta($comment_ID, 'title', true);
}
Run Code Online (Sandbox Code Playgroud)
这里 WP_Comment_Query 可能更有用,因为它有很多参数。
| 归档时间: |
|
| 查看次数: |
2647 次 |
| 最近记录: |