Php WooCommerce:在主页上随机显示一些评论
我想有5个随机评论出现在我的主页上,每次有人访问 我找到了一些代码来获取所有评论:Php WooCommerce:在主页上随机显示一些评论,php,sql,wordpress,woocommerce,shortcode,Php,Sql,Wordpress,Woocommerce,Shortcode,我想有5个随机评论出现在我的主页上,每次有人访问 我找到了一些代码来获取所有评论: //add get product reviews to homepage function get_woo_reviews() { $count = 0; $html_r = ""; $title=""; $args = array( 'post_type' => 'product' ); $comments_query = new WP
//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]
添加到这里时,它就可以正常工作了
我如何更改此设置以仅获得5个随机评论
另外,我现在如何格式化此页面,使其仅包含5条评论,并能够更改页面上评论的外观(间距、字体等)?添加以下代码片段-
function get_woo_reviews()
{
$comments = get_comments(
array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
)
);
shuffle($comments);
$comments = array_slice( $comments, 0, 5 );
$html = '<ul>';
foreach( $comments as $comment ) :
$html .= '<li><h2>'.get_the_title( $comment->comment_post_ID ).'</h2>';
$html .= '<p>' .$comment->comment_content.'</p>';
$html .= "<p>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</p></li>";
endforeach;
$html .= '</ul>';
ob_start();
echo $html;
$html = ob_get_contents();
ob_end_clean();
return $html;
}
add_shortcode('woo_reviews', 'get_woo_reviews');
函数get\u woo\u reviews()
{
$comments=获取注释(
排列(
“状态”=>“批准”,
“发布状态”=>“发布”,
“post_类型”=>“产品”,
)
);
洗牌(评论);
$comments=array\u slice($comments,0,5);
$html=“”;
foreach($comments作为$comment):
$html.='- ”。获取标题($comment->comment\u post\u ID)。“”;
$html.=''.$comment->comment\u content.';
$html.=“发布人:“$comment->comment\u author.”于“$comment->comment\u date.”
”;
endforeach;
$html.='
';
ob_start();
echo$html;
$html=ob_get_contents();
ob_end_clean();
返回$html;
}
添加快捷码(“woo_评论”、“get_woo_评论”);
添加以下代码片段-
function get_woo_reviews()
{
$comments = get_comments(
array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
)
);
shuffle($comments);
$comments = array_slice( $comments, 0, 5 );
$html = '<ul>';
foreach( $comments as $comment ) :
$html .= '<li><h2>'.get_the_title( $comment->comment_post_ID ).'</h2>';
$html .= '<p>' .$comment->comment_content.'</p>';
$html .= "<p>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</p></li>";
endforeach;
$html .= '</ul>';
ob_start();
echo $html;
$html = ob_get_contents();
ob_end_clean();
return $html;
}
add_shortcode('woo_reviews', 'get_woo_reviews');
函数get\u woo\u reviews()
{
$comments=获取注释(
排列(
“状态”=>“批准”,
“发布状态”=>“发布”,
“post_类型”=>“产品”,
)
);
洗牌(评论);
$comments=array\u slice($comments,0,5);
$html=“”;
foreach($comments作为$comment):
$html.='- ”。获取标题($comment->comment\u post\u ID)。“”;
$html.=''.$comment->comment\u content.';
$html.=“发布人:“$comment->comment\u author.”于“$comment->comment\u date.”
”;
endforeach;
$html.='
';
ob_start();
echo$html;
$html=ob_get_contents();
ob_end_clean();
返回$html;
}
添加快捷码(“woo_评论”、“get_woo_评论”);
使用注释WP\u comment\u Query
,注释不能以随机顺序排列。因此,您需要使用一个简单的轻量级SQL查询,使用专用的WordPress类
在下面的代码中,您可以更改样式和html结构以获得所需的输出。您还可以使用可用的快捷码参数“limit(默认设置为5)设置要以随机顺序显示的评论数:
add_shortcode('woo_reviews','get_random_woo_reviews');
函数获取随机访问评论($atts){
//短代码属性
$atts=短码_atts(数组)(
'limit'=>'5',//获取结果($wpdb->prepare('
挑选*
从{$wpdb->prefix}注释c
内部连接{$wpdb->prefix}在c.comment\u post\u ID=p.ID上发布p
其中c.comment\u type='review'和p.post\u status='publish'
按兰德订购()限制%d
“,intval(esc_attr($atts['limit']));
ob_start();//开始缓冲
##CSS应用样式
?>
ul.product-reviews,ul.product-reviews li{列表样式:无;边距:0;填充:0;线条高度:正常;}
ul.product-li{显示:块;最大宽度:200px,填充:10px;显示:内联块;垂直对齐:文本顶部;}
ul.product-reviews li.标题{字体大小:1.2em;}
ul.product-reviews li.内容{最大宽度:180px;字体大小:0.9em;页边距底部:6px;}
ul.product-reviews li.作者,ul.product-reviews li.日期{显示:块;字体大小:0.75em;}
-
对于注释WP\u comment\u Query
,注释不能是随机顺序的。因此,您需要使用一个简单的轻量级SQL查询,使用专用的WordPress类
在以下代码中,您可以更改样式和html结构以获得所需的输出。您还可以使用可用的快捷码参数“limit”(默认设置为5)设置要以随机顺序显示的评论数:
add_shortcode('woo_reviews','get_random_woo_reviews');
函数获取随机访问评论($atts){
//短代码属性
$atts=短码_atts(数组)(
'limit'=>'5',//获取结果($wpdb->prepare('
挑选*
从{$wpdb->prefix}注释c
内部连接{$wpdb->prefix}在c.comment\u post\u ID=p.ID上发布p
其中c.comment\u type='review'和p.post\u status='publish'
按兰德订购()限制%d
“,intval(esc_attr($atts['limit']));
ob_start();//开始缓冲
##CSS应用样式
?>
ul.product-reviews,ul.product-reviews li{列表样式:无;边距:0;填充:0;线条高度:正常;}
ul.product-li{显示:块;最大宽度:200px,填充:10px;显示:内联块;垂直对齐:文本顶部;}
ul.product-reviews li.标题{字体大小:1.2em;}
ul.product-reviews li.内容{最大宽度:180px;字体大小:0.9em;页边距底部:6px;}
ul.product-reviews li.作者,ul.product-reviews li.日期{显示:块;字体大小:0.75em;}
-