Can wp_list_comments output into variable?

Is there any way how to output wp_list_comments() into a variable?

Something like $output = wp_list_comments(array('reverse_top_level' => false ), $comments);

Read More

I am writing a plugin and it would be handy to use standard WordPress function instead of writing my own.

Related posts

2 comments

  1. In the default usage this is impossible due to the nature of the default comment walker which always directly outputs. But the function allows to provide a custom walker.

    Further reading about custom walkers:
    Codex Class reference
    example custom walker class

    You could also use output buffering to save it into a variable (this is considered to be dirty):

    ob_start();
    wp_list_comments(array('reverse_top_level' => false ), $comments);
    $variable = ob_get_clean();
    
  2. According to Function Reference for wp_list_comments, you can set the arg Echo to false to return the list.

    I imagine the default walker will not echo the code if this is set to false.

    wp_list_comments(array('reverse_top_level' => false, 'echo' => false), $comments);
    

Comments are closed.