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 have a problem with the numbering of my comments when they extend to more than one page. On the new page the numbering starts from again from 1. Also, on page 2 the pingbacks/trackbacks don't appear anymore.

This is the code I'm using in comments.php:

<?php if ( have_comments() ) : ?>
    <h3 class="com"><span class="com-titlu"><?php comments_number('Niciun comentariu', 'Un comentariu', '% comentarii' );?></span> la: <?php the_title(); ?></h3>
    <ol class="commentlist">
        <?php wp_list_comments(array(
          'callback'=>'advanced_comment',
          'style'=>'ol',
          'type'=>'comment'
          ));
        ?>
    </ol>
    <?php 
      if ( $num = t5_count_pings() )
        {
    ?>
     <h3 id="pingbacks">
       <?php printf( _n( 'One pingback', '%d pingbacks', $num, 't5_theme' ), $num ); ?>
     </h3>
    <ol class="pingback">
    <?php wp_list_comments(array (
            'type'     => 'pings',
            'style'    => 'ul',
            'callback' => 'list_pings'
        ));
    ?></ol>
    <?php } ?>

And these are the functions I used in functions.php:

<?php function advanced_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
   <div class="comment-author vcard">
     <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
       <div class="comment-meta"<a href="<?php the_author_meta( 'user_url'); ?>"><?php printf(__('%s'), get_comment_author_link()) ?></a> 
       <span class="com-date"> a scris pe: <small><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?><?php edit_comment_link(__('(Edit)'),'  ','') ?></small></span></div>
     </div>
     <div class="clear"></div>
     <?php if ($comment->comment_approved == '0') : ?>
       <em><?php _e('Your comment is awaiting moderation.') ?></em><br />
     <?php endif; ?>
     <div class="comment-text"> 
         <?php comment_text() ?>
     </div>
   <div class="reply">
      <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
   </div>
   <div class="clear"></div>
<?php } ?>
<?php add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
return count($comments_by_type['comment']);
} else {
return $count;
}
}
?>
<?php 
/**
 * Count amount of pingbacks + trackbacks for a post.
 *
 * @param int $post_id Post ID for comment query. Default is current post.
 * @return int
 */
function t5_count_pings( $post_id = NULL )
{
    $pings    = 0;
    $comments = FALSE;
    if ( NULL !== $post_id )
    {
        $comments = get_comments(
            array (
                'post_id' => $post_id, # Note: post_ID will not work!
                'status'  => 'approve'
            )
        );
    }
    elseif ( ! empty ( $GLOBALS['wp_query']->comments ) )
    {
        $comments = $GLOBALS['wp_query']->comments;
    }
    if ( ! $comments )
        return 0;
    foreach ( $comments as $c )
        if ( in_array ( $c->comment_type, array ( 'pingback', 'trackback' ) ) )
            $pings += 1; 
    return $pings;
}
?>

Problems: 1) Trackbacks and pingbacks don't appear on each page of comments. 2) The numbering of comments starts over on each new page.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

To fix the numbering, you'll have to modify the first code to reset numbering via CSS. This way (untested, but you'll catch the idea ;) ).

<?php if ( have_comments() ) : ?>
    <h3 class="com"><span class="com-titlu"><?php comments_number('Niciun comentariu', 'Un comentariu', '% comentarii' );?></span> la: <?php the_title(); ?></h3>
<?php $number = intval(get_query_var( 'cpage' )) * intval(get_query_var( 'comments_per_page' )) - intval( get_query_var( 'comments_per_page' ) ); ?>
    <ol class="commentlist" style="counter-reset: item <?php echo $number; ?>">
        <?php wp_list_comments...

The idea of used algorithm for getting starting number is that if your are on page 2 displaying 5 comments per page, you'll start with 6.

Than to determine the first number, you have to multiply the page number (2) with comments per page (5) - you'll get a result of 10.

The first number have to be 6, so you'll have to subtract comments per page (5) from the result and use the final result as { counter-reset: item FINAL_RESULT }

CSS starts numbering from a number following after the one used in this rule. So 5 is OK.

2*5-5 = 5 => starts with 6

3*5-5 = 10 => starts with 11

If you need the reverse numbering, this is going to be much more complicated as CSS probably does not offer a solution. But you can check this question and answer. Also the algorithm for determining first number is going to be different.

share|improve this answer
    
Your script is working, i can see the counter in firebug but I think I have some problems with the CSS. The problem is that I can get the numbering to work correctly. This is the code I've used: .depth-1:before { content: "" counter(item, decimal) "."; counter-increment: item 1; }. As a result each comment has the number "1." in front. –  George Grigorita Feb 10 '13 at 22:00
    
It would help if I had an option to see the outputed HTML and CSS somewhere - jsfidlle? –  david.binda Feb 11 '13 at 7:36
    
I solved it in the meantime. Thanks for helping, like I said, the script worked great! –  George Grigorita Feb 11 '13 at 11:14

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.