• 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.

  • The topic ‘Comment as post – understanding wp_list_comments’ is closed to new replies.