1

I need to display comments starting from the second, omitting the first. With posts I did it using "offset" but I can't find anything similar for comments. Since I'm using threaded comments, must start from the second comment that is not a child (only parent comments). I'm using wp_list_comments with a callback.

EDIT2: this is actual comments.php code, after replies:

      <?php 
$i = 2;
if ( have_comments() ) : 
if ( $i > 1 ) :
?>
    <div id="comments">
    <h3 id="comments-title" class="color-<?php $category = get_the_category(); echo $category[0]->cat_name;?>">
</h3>


    <?php 

     global $wp_query; 
     $comments_arr = $wp_query->comments; 
     $comments_arr = sort( $comments_arr); 
     $comments_arr = array_pop( $comments_arr );
     $comments_arr = usort($comment_arr, 'comment_comparator');                 
     wp_list_comments('callback=gtcn_basic_callback', $comments_arr); 

    ?>


    <?php
    $i++; // count up
    endif; // If $i > 1
    else : if ( ! comments_open() ) : ?>
            <p><?php _e( 'Comments are closed.' ); ?></p>

    <?php 
      endif; // end ! comments_open() 
endif; // end have_comments()  ?>
</div>
<?php comment_form(); ?>

Result: comments are not anymore ordered by karma (comment_comparator function) and the first comment (comment with top karma that I want to hide) is still displayed.

1 Answer 1

0
<?php 
global $wp_query, $post; 
$comments_arr = $wp_query->comments; 
$comments_arr = sort( $comments_arr); 
$comments_arr = array_pop( $comments_arr );
$comments_arr = usort($comment_arr, 'comment_comparator');

$comment_count = get_comment_count($post->ID);

 ?>
<h3 id="comments-title"><?php
    printf(
    _n( 'One Response to %2$s', '%1$s Responses to %2$s', get_comments_number(), 'YOUR_THEMES_TEXTDOMAIN' ),
    number_format_i18n( get_comments_number() ),
    '<em>' . get_the_title() . '</em>' 
);
?></h3>
if ( have_comments() ) : 
?><ul class="commentlist"><?php
for ( $i = 0; $i < $comment_count; $i++ )
{
    // do stuff: 
        // This shows you everything you got inside the comments array
        // Call it like this: echo $comments_arr[$i]['comment_author'];
        var_dump( $comments_arr[$i] ); 
    // end do stuff
} // endforeach;
?></ul><?php

elseif ('open' == $post->comment_status) :
    // If there are no comments, but status = open
    ?><p class="nocomments"><?php _e( 'No Comments.', 'YOUR_THEMES_TEXTDOMAIN' ); ?></p><?php
endif; // If $i > 1

else : // or, if comments are not allowed:
    if ( ! comments_open() ) : 
    ?>
        <p class="nocomments"><?php _e( 'Comments are closed.', 'YOUR_THEMES_TEXTDOMAIN' ); ?></p>
    <?php 
    endif; // end ! comments_open() 
endif; // end have_comments() 
?>

Ad your current loop:

No need for $counter = 2; (called nowhere).

If you already got your $comments_arr, why not simply:

$comments_arr = sort( $comments_arr); 
$comments_arr = array_pop( $comments_arr ); 
$comments_arr = usort( $comments_arr );

OR

$comments_arr = unset( $comments_arr[0] ); 
$comments_arr = usort( $comments_arr );
11
  • Thanks kaiser, but it's not working for me. It broke the layout and comments are not displayed anymore. I added my actual comments.php code to the question.
    – Andycap
    May 18, 2011 at 22:45
  • It should. There's close to no difference to your code and it's taken from twenty ten. I guess you have forgotten to close some html tag before the comments loop. See edit.
    – kaiser
    May 19, 2011 at 12:07
  • but if ( $i > 1 ) : should not be inside a php tag?
    – Andycap
    May 19, 2011 at 22:30
  • You're right. That was my fault. Most was copy/paste. Just try again after the edit and try to find and resolve errors that depend on <?php ?> as you did before :)
    – kaiser
    May 19, 2011 at 23:37
  • This is making me nuts. It looks simple, I understand it but, when I correctly put the code, the comments are not displayed anymore, only the comment form. If I comment if ( $i > 1 ) : and endif; // If $i > 1 everything starts to work again.
    – Andycap
    May 25, 2011 at 23:25

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.