I have to load multiple posts on a single page. The situation is different. The client ask to reorder the posts, that the current post of the category becomes first and all the rest are next. Then on scroll I load the next posts (infinite scroll effect). So what I do is, I query the database, get the posts, find my current post in the returned collection, and create a new array with the already reordered posts and I load one post when the user reaches the end of the page, on scroll.

So far so good...

But my task is to show a corresponding comment form to each loaded post. So if I have 20 posts on my single page, I have 20 comment forms beneath each post. I can't find a way of doing this. The wordpress function comment_form(), comments_template(), they all render html and not return, which is the problem. I need to return the html of that form so I can insert it to my collection and render it along with the post on scroll.

Not sure what code I have to provide there, because I don't have any. If you need me to provide something, please ask.


Tsvetan Dimitrov

2 Answers 2


You can use output buffering to achieve this effect:

function get_comments_form() {
    $commentsform = ob_get_contents();
    return $commentsform;

$commentsform = get_comments_form();
  • Thank you for the answer. This approach is good, not sure if the right one.
    – Dimitrov
    Apr 7, 2016 at 9:15
  • Unfortunately given the way the comments functions are written I can't see another feasible solution.
    – majick
    Apr 7, 2016 at 10:37

You can try the following code logic

function THEMENAME_comment($comment, $args, $depth) {
  $GLOBALS['comment'] = $comment;
  *your comment display code*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.