0

I'm creating a site where a central question will be posed on the front page with excerpted first-tier comments linking to a page with the full first-tier comment, presented almost as a post, with nested replies to that comment below. Why a comment as a post? Seemed like a good way to let users author content without giving access to the back end.

Where I'm having trouble is with those nested replies to a specific comment. I'm trying to keep things simple and use wp functions where possible. My approach was to build an array of all the replies to the specific comment and then pass that array in to wp_list_comments. Which all works, except for the nesting part; all replies are treated as at the same level, or tier. Obviously, I don't fully understand how wp_list_comments works.

Here's my loop to create the array of all replies to a specific comment (passed as $_GET[comment_id]):

$my_comments = array();
$args = array(
    'type' => 'comment',
    'status' => 'approve',
    'parent' => $_GET[comment_id],
);
$my_comments1 = get_comments($args);
if(!empty($my_comments1)) {
    $my_comments = array_merge($my_comments, $my_comments1);
    $x=1;
    $finished = false;
    while(! $finished) {
        foreach(${'my_comments'.$x} as $my_comment) {
            if($my_comment->comment_ID) {
                $args = array(
                    'type' => 'comment',
                    'status' => 'approve',
                    'parent' => $my_comment->comment_ID,
                );
                $x++;
                ${'my_comments'.$x} = get_comments($args);
                if(!empty(${'my_comments'.$x})) {
                    $my_comments = array_merge($my_comments, ${'my_comments'.$x});
                } 
                elseif(empty(${'my_comments'.$x})) {$finished = true;}
            }
        }
    }
} // if not empty

And here's how I'm displaying:

<ol class="commentlist">
    <?php wp_list_comments( array( 'style' => 'ol' ), $my_comments); ?>
</ol><!-- .commentlist -->

Any insight on how wp_list_comments works would be appreciated, as would any other big idea approaches to my goal of a first-tier comment presented as a post with replies to that comment nested below.

4 Answers 4

1

checkout the comment_reply_link function...

An example would be:

comment_reply_link(
    array_merge(
        $args,
        array(
            'depth' => $depth,
            'max_depth' => $args['max_depth'],
        )
    )
);
1
  • Thanks for the suggestion but I can't seem to get it to display any link at all, no matter what I use for parameters. Plus, not sure how this will help display nested comments. Sorry to be slow but any ideas?
    – user22831
    Oct 24, 2012 at 0:18
0

Do you have enabled nested comments under Settings - Discussion - Enable threaded (nested) comments ?

0

For a propper display of nested comments, you have to display the parent comment:

    $parent      = filter_input( INPUT_GET, 'comment_id', FILTER_SANITIZE_NUMBER_INT );
    $my_comments = array( get_comment( $parent ) );

    $args = array(
        'type' => 'comment',
        'status' => 'approve',
        'parent' => $parent,
    );

    $my_comments1 = get_comments($args);
    [...]

echo '<ol class="comments-area">';
wp_list_comments( array( 'style' => 'ol' ), $my_comments );
echo '</ol>';

And the comments are in wrong order (older replies first). Add an array_reverse():

[...]
if(!empty(${'my_comments'.$x})) {
  $my_comments = array_merge($my_comments, array_reverse( ${'my_comments'.$x} ) );
}
[...]

Now solving the problem with the parent comment. The parent comment is displayed as post, but it is needed to display the replies propperly. You have to "remove" the parent comment from the list.

You can hide it with css:

<style>
ol.comments-area > li:first-child { list-style-type: none }
ol.comments-area > li:first-child > div { display:none }
</style>

Or use jQuery/JavaScript to remove the parent coment.

I hope this will help you.

0

Rather than hacking the comment system into doing something it isn't meant to do, I'd create a Custom Post Type for the user generated content and expose a form on the front-end of the site that allows the user to generate content of that CPT's type. This can be done pretty easily with a number of form plugins, such as Gravity Forms, Ninja Forms or Caldera Forms (some of them will require paid add-ons for this to work). You can also write some custom code that does the same thing, it will just take a bit more work. I'd recommend looking at this tutorial using CMB2 or the code here for a good starting point.

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.