admin管理员组

文章数量:1415491

Im trying to display a comment box inside a foreach loop however the comment_form function outputs before anything else. Here is the code I'm trying. Everything is displaying, just not in the right place. Any ideas or diff methods?

                // get comments
            $achievment_post .= '<ol class="commentlist">';
              //Gather comments for a specific page/post 
              $comments = get_comments(array(
                'post_id' => $achievment->ID,
                 'status' => 'approve'
              ));
              $achievment_post .= wp_list_comments(array(
                'per_page' => 10, // Allow comment pagination
                 'reverse_top_level' => false //Show the latest comments at the top of the list
               ), $comments);

              $achievment_post .= '</ol>';

            // comment box

            $achievment_post .= comment_form($args , $achievment->ID);

Im trying to display a comment box inside a foreach loop however the comment_form function outputs before anything else. Here is the code I'm trying. Everything is displaying, just not in the right place. Any ideas or diff methods?

                // get comments
            $achievment_post .= '<ol class="commentlist">';
              //Gather comments for a specific page/post 
              $comments = get_comments(array(
                'post_id' => $achievment->ID,
                 'status' => 'approve'
              ));
              $achievment_post .= wp_list_comments(array(
                'per_page' => 10, // Allow comment pagination
                 'reverse_top_level' => false //Show the latest comments at the top of the list
               ), $comments);

              $achievment_post .= '</ol>';

            // comment box

            $achievment_post .= comment_form($args , $achievment->ID);
Share Improve this question asked Sep 4, 2019 at 14:36 Silvester VellaSilvester Vella 155 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This is because you're collecting the HTML in a variable before you output, but comment_form does not return HTML, it immediatley renders.

So, eliminate the $achievement_post variable, and output directly with echo instead, and things will work more as you expect

本文标签: commentsNeed to output commentform() function inside a foreach loop