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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Easy 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