WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying comment number for each comment in 2.7 (12 posts)

  1. pannpann
    Member
    Posted 5 years ago #

    I've been unable to find a way to output the comment number for each comment in 2.7. Here's the problem:

    - Using list-type with CSS is not an option as I want more control over the number for styling
    - Using the comment ID messes everything up if a comment is deleted or not approved etc.
    - I have not succeeded in adding an incremental counter within a loop with the new comment listing function. I guess this is the path I'll have to go - the problem is how to actually do it.

    Any ideas?

  2. pannpann
    Member
    Posted 5 years ago #

    Anyone?

  3. blackmane101
    Member
    Posted 5 years ago #

    I am having the same trouble. I can't for the life of me figure it out. I could do it in the earlier versions. Any one out there have any ideas?

  4. Dantes100
    Member
    Posted 5 years ago #

    I am trying to figure this out for over two weeks now. Still nothing.

    Is it even possible with WP 2.7?

  5. pannpann
    Member
    Posted 5 years ago #

    Thank you, twitter (mollstam)! This seems to do the trick. Add this in your start_el function in comment_template.php:

    # Comment counter
    global $comment_num;
    if(isset($comment_num)) {
        $comment_num++;
    } else { $comment_num = 1; }

    You could also add a zero before any single digit number:

    # Add 0 if less than 10
    if ($comment_num < 10) $comment_num = "0".$comment_num;

    Finally print the number as you prefer, for example:

    <?php echo $comment_num ?>
  6. Dantes100
    Member
    Posted 5 years ago #

    Hi pannpann,

    Thank you, I've just tried it.

    It works, but not entirely:

    If you have more than one comment page, the numbering starts on every page with "1" again.

    Any fix to this, or maybe I just inserted the code wrong (I have a completely different template).

    Eddie

  7. blackmane101
    Member
    Posted 5 years ago #

    What you have said works fine, but as Dantes mentioned there is a problem if you have more than one page. Any solutions found?

  8. pannpann
    Member
    Posted 5 years ago #

    Thanks, I didn't even notice that. I came up with a fix — it's a bit rough, but it works great for me. Add this before the #Comment Counter:

    #Get the current comment page and calculate preceeding comments
    		if ( '' === $args['per_page'] && get_option('page_comments') )
    			$args['per_page'] = get_option('comments_per_page');
    
    		if ( empty($args['per_page']) ) {
    			$args['per_page'] = 0;
    			$args['page'] = 0;
    		}
    
    		if ( $args['per_page'] ) {
    			if ( '' == $args['page'] )
    				$args['page'] = get_query_var('cpage');
    		}
    		$ccomp = ($args['page']-1) * $args['per_page'] ;

    The final calculation, including the comment counter, looks like this:

    #Get the current comment page and calculate preceeding comments
    		if ( '' === $args['per_page'] && get_option('page_comments') )
    			$args['per_page'] = get_option('comments_per_page');
    
    		if ( empty($args['per_page']) ) {
    			$args['per_page'] = 0;
    			$args['page'] = 0;
    		}
    
    		if ( $args['per_page'] ) {
    			if ( '' == $args['page'] )
    				$args['page'] = get_query_var('cpage');
    		}
    		$ccomp = ($args['page']-1) * $args['per_page'] ;
    
    		# Comment counter
    		global $comment_num;
    		if(isset($comment_num)) {
    		    $comment_num++;
    		} else { $comment_num = 1; }

    The comment compensation is stored in $ccomp, so don't forget to add it when printing the comment number:

    <?php echo $comment_num + $ccomp ?>

    Hope this works for you!

  9. Dantes100
    Member
    Posted 5 years ago #

    @pannpann

    I would like to thank you for your outstanding work.

    I've just implemented it, this works great for me!

    Good job, thank you very much.

    Eddie

  10. pannpann
    Member
    Posted 5 years ago #

    Thank you Dantes, I'm glad it worked!

    For questions or further support on this hack, the easiest way to find me is through twitter: pannpann.

    Finally, if someone is aware of a better way to do this, any tips or feedback would be terrific.

  11. GregMulhauser
    Member
    Posted 5 years ago #

    Hi folks,

    I think this can get quite tricky when it comes to threaded comments, as well as if you choose to display comments in descending rather than ascending date order.

    If you can bear some shameless plugin plugging, I've just released a plugin that can take care of all the details for you, enabling you to drop in comment numbers wherever you'd like them to appear in your comments:

    Greg's Threaded Comment Numbering

    It works in conjunction with the new comment display function introduced in 2.7, so it does require some basic retro-fitting for themes still using the older comment loop. Give it a whirl and see what you think!

    All the best,
    Greg

  12. ubergiant
    Member
    Posted 5 years ago #

    Good work pannpann. I've been looking for a solution to this after a lot of searching. I don't understand anything you did, but it works. ;)

Topic Closed

This topic has been closed to new replies.

About this Topic