Php Woocommerce最新评论
我试图在我的测试页面上获取任何产品的最新评论/评论,目前为止,我将其视为一篇帖子,得到如下内容:Php Woocommerce最新评论,php,wordpress,woocommerce,review,Php,Wordpress,Woocommerce,Review,我试图在我的测试页面上获取任何产品的最新评论/评论,目前为止,我将其视为一篇帖子,得到如下内容: <?php $args = array ('post_type' => 'product'); $comments = get_comments( $args ); wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments); ?> <?php get_
<?php
$args = array ('post_type' => 'product');
$comments = get_comments( $args );
wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
?>
<?php get_comments( $args ); ?>
非常感谢您的帮助。试试以下方法:
$number_of_reviews = 10; //How many reviews you want to retrieve
$reviews = get_comments( array( 'number' => $number_of_reviews, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product' ) );
echo "<ul>";
foreach ( (array) $reviews as $review ) {
$_product = WC_get_product( $review->comment_post_ID );
$rating = intval( get_comment_meta( $review->comment_ID, 'rating', true ) );
echo '<li style="list-style:none"><a href="' . esc_url( get_comment_link( $review->comment_ID ) ) . '">';
echo $_product->get_title() . '</a> ';
for($i=0;$i<$rating;$i++){
echo "<span style='color:grey'>★</span>";
}
echo "<p>".$review->comment_content." - ".get_comment_author($review->comment_ID)."</p>";
echo '</li><br><br>';
}
echo "</ul>";
$number of_reviews=10//您要检索多少评论
$reviews=get_comments(数组('number'=>'number_of_reviews','status'=>'approve','post_status'=>'publish','post_type'=>'product');
回声“”;
foreach((数组)$reviews作为$review){
$\u product=WC\u get\u product($review->comment\u post\u ID);
$rating=intval(获取评论元($review->comment_ID,'rating',true));
echo'- ;
对于($i=0;$icomment\u content.“-”。获取作者的评论($review->comment\u ID)。“”;
回音“
”;
}
回声“
”;
注意:您可以随时根据需要和CSS修改代码。这是我为获取产品的最新评论所做的:
function display_product_review() {
global $product;
$comments = get_approved_comments( $product->id );
$product_link = '/product/' . $product->post->post_name . "/#tab-reviews/";
if ( !empty ($comments) ) {
echo $comments[0]->comment_content . '<br><a href="'. $product_link . '">Read more reviews...</a>';
} else {
echo "There are no reviews for this product yet. Would you like to <a href='" . $product_link ."'>add your own review</a>?";
}
}
功能显示\产品\审查(){
全球$产品;
$comments=get_approved_comments($product->id);
$product_link='/product/'.$product->post->post_name.“/#选项卡评论/”;
如果(!空($comments)){
echo$comments[0]->comment_内容。“
”;
}否则{
echo“目前还没有对该产品的评论。您愿意吗?”;
}
}
唯一的缺点是get_approved_comments只有一个参数,即post_id,因此它确实会拉取对该产品的所有评论。这可能不是最干净的解决方案,但它确实对我有效。它切断了该行的所有内容,并大声吼叫,比如未关闭的php标记或其他我试图在sta上添加php标记的内容rt和end,第1部分和第2部分仍然是失败的页面。你是如何实现我的代码的?在哪里?第1部分和第2部分是什么?index.php,在一个div中,第1行和第2行开始$1部分,其余的第2部分同样的代码对我来说很好。因此,如果你想要有人帮助,你需要在你的问题中发布相关内容,然后有人可以帮助你。你也可以试着在我的代码中调试,比如放入
var\u dump($reviews);
来查看它检索了多少条评论。这可能是你调试的起点。