admin管理员组文章数量:1310081
It is necessary to display comments on the page randomly using a shortcode. Now I output comments using a shortcode using the code:
add_action('wp_head', function (){
global $postPage, $post;
$postPage = $post;
});
add_shortcode('comments_block',function($attr, $content){
global $post,$postPage;
$savePost = $post;
$post = $postPage;
ob_start();
comments_template();
$output = ob_get_contents();
ob_end_clean();
$post = $savePost;
return $output;
});
This displays the comments and below them a form for adding comments - everything is fine. But comments need to be displayed randomly (now they are standard by date).
I also tried to combine this code with the one presented here How can I show comments in random order? , but I don't have enough knowledge in this regard(
I will be grateful for any help in this regard.
It is necessary to display comments on the page randomly using a shortcode. Now I output comments using a shortcode using the code:
add_action('wp_head', function (){
global $postPage, $post;
$postPage = $post;
});
add_shortcode('comments_block',function($attr, $content){
global $post,$postPage;
$savePost = $post;
$post = $postPage;
ob_start();
comments_template();
$output = ob_get_contents();
ob_end_clean();
$post = $savePost;
return $output;
});
This displays the comments and below them a form for adding comments - everything is fine. But comments need to be displayed randomly (now they are standard by date).
I also tried to combine this code with the one presented here How can I show comments in random order? , but I don't have enough knowledge in this regard(
I will be grateful for any help in this regard.
Share Improve this question asked Jan 7, 2021 at 11:46 Sergey PervushinSergey Pervushin 1232 bronze badges1 Answer
Reset to default 0Easy thing would be to override comment query and set orderby
property to rand
.
function wpse381090_comment_query_rand( $args ) {
$args['orderby'] = 'rand';
return $args;
}
add_filter('comments_template_query_args', 'wpse381090_comment_query_rand');
本文标签: orderRandom output of comments using a shortcode
版权声明:本文标题:order - Random output of comments using a shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741853523a2401206.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论