Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    78
    Thanks
    17
    Thanked 1 Time in 1 Post

    Trying to add numbered comments in the comment section with $commentcount++

    I'm building this site on Wordpress and I'm trying to make the comments numbered for the comments section by setting a variable and using $commentcount++ . You can see what I mean here:

    As you can see, all of the numbers are "1" when they should be adding up as new comments get submitted. What am I doing wrong?

    Here's what I have at the top of comments.php:

    Code:
    <?php
    
    // Do not delete these lines
    	if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
    		die ('Please do not load this page directly. Thanks!');
    
    	if ( post_password_required() ) { ?>
    		<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
    	<?php
    		return;
    	}
    
    	$commentcount = 1;
    
    ?>
    Here's what I put in my functions.php file:

    Code:
    function format_comment($comment, $args, $depth) {
       $GLOBALS['comment'] = $comment; $commentcount = 1;?>
       <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
    		<span class="comment-number">
    			<?php echo $commentcount++;?>
    		</span>
    		<div class="comment-avatar">
    			<?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
    		</div>
    		<div class="comment-text">
    			<div class="comment-author">
    				<?php printf(__('<span>%s</span>'), get_comment_author_link()) ?>
    			</div>
    			<div class="comment-date">
    				<?php echo get_comment_date(); ?>
    			</div>
    			<?php if ($comment->comment_approved == '0') : ?>
    				<em><?php _e('Your comment is awaiting moderation.') ?></em>
    			<?php endif; ?>
    
    			<?php edit_comment_link(__('(Edit)'),'  ','') ?>
    
    			<?php comment_text() ?>
    		</div>
    <?php
            }
    Any help would be appreciated. Thanks in advance.
    Last edited by cpkid2; 02-03-2010 at 07:13 AM.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    where is the script the loops through the comments to display them?

    format_comment() only displays one comment at a time. so each time you are resetting the counter back to 1.

    $commentcount = 1;
    Last edited by angst; 02-02-2010 at 10:13 PM. Reason: typoz.

  • #3
    New Coder
    Join Date
    Sep 2009
    Posts
    78
    Thanks
    17
    Thanked 1 Time in 1 Post
    The script that loops through the comments is the function.php file. I put "<?php echo $commentcount++;?>" on line 5.

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Change
    PHP Code:
    $commentcount 1;
    // to
    static $commentcount 1
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    New Coder
    Join Date
    Sep 2009
    Posts
    78
    Thanks
    17
    Thanked 1 Time in 1 Post
    ^I just tried that but it didn't work.

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    I would put the counter in one file, not in two files. It makes it easier to debug. The counter has to be setup like:

    Code:
    DB Query
    $counter = 1;
    
    while loop {
    echo $counter
    echo db data
    $counter++;
    }


    ---------
    Leonard Whistler

  • #7
    New Coder
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    2
    Thanked 3 Times in 3 Posts
    PHP Code:
    <?php

    $commentcount 
    1;

    // Do not delete these lines
        
    if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
            die (
    'Please do not load this page directly. Thanks!');

        if ( 
    post_password_required() ) { ?>
            <p class="nocomments">This post is password protected. Enter the password to view comments.</p>
        <?php
            
    return;
        }

    ?>
    PHP Code:
    function format_comment($comment, $args, $depth) {
       $GLOBALS['comment'] = $comment; ?>
       <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
            <span class="comment-number">
                <?php echo $commentcount++;?>
            </span>
            <div class="comment-avatar">
                <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
            </div>
            <div class="comment-text">
                <div class="comment-author">
                    <?php printf(__('<span>%s</span>'), get_comment_author_link()) ?>
                </div>
                <div class="comment-date">
                    <?php echo get_comment_date(); ?>
                </div>
                <?php if ($comment->comment_approved == '0') : ?>
                    <em><?php _e('Your comment is awaiting moderation.'?></em>
                <?php endif; ?>

                <?php edit_comment_link(__('(Edit)'),'  ',''?>

                <?php comment_text() ?>
            </div>
    <?php
    $commentcount
    ++;
            }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •