Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

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

share|improve this answer

checkout the comment_reply_link function...

An example would be:

comment_reply_link(
    array_merge(
        $args,
        array(
            'depth' => $depth,
            'max_depth' => $args['max_depth'],
        )
    )
);
share|improve this answer
    
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 '12 at 0:18

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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