• Resolved Resilient

    (@resilient)


    Hey!

    Just a quick question asking for advice and any suggestions on how to implement a feature where the comment number is displayed for each individual comment. I understand comments have individual ID’s, meaning for every post they would display their own unique ID number, but I’m wondering whether it would be possible to have the first comment start from 01 and increment on the comments below.

    I’m not sure how I can be more specific, so to back my query I’ve provided a screenshot of a snippet of a layout I’ve designed in photoshop, with an orange line circling the area I’m talking about.

    https://www.endlesstears.net/post-comment.jpg

Viewing 12 replies - 1 through 12 (of 12 total)
  • <?php comment_ID(); ?>

    Read more here.

    Thread Starter Resilient

    (@resilient)

    Many thanks for that, however, that deals with the actual comment ID and not the comment number out of the number of however many comments there are on a single post.

    An example of what I’m looking for can be seen here; https://www.xeenat.com/2006/09/05/and-so-it-begins/#comments

    Notice, to the left of every visitor name that has submitted a comment, there is a number, starting from 1 and working it’s way incremented by 1 for each comment below.

    Yes. The display of comments is just a regular PHP foreach loop. So just setup a variable to count as you go through the loop.

    $commentNumber = 0;
    foreach ($comments as $comment) :

    $commentNumber++;
    echo ‘Comment number is ‘ + $commentNumber;

    etc

    Hi,

    I’m trying to do the same thing.

    PHP Newbie here!

    Well, I have this atm but it starts with zero. How to start with 1?

    <?php
    $commentNumber;
    echo $commentNumber;
    $commentNumber++;
    ?>

    And I also tried:

    <?php
    $commentNumber = 0;
    foreach ($comments as $comment) :
    $commentNumber++;
    echo $commentNumber;
    endforeach;
    ?>

    Say there are ten (10) comments then in results in simply:

    12345678910

    Any ideas?

    Thanks,

    Will

    Bump, anyone have any ideas?

    war59312, I’m not sure I understand your problem. You say you want to know how to get $commentNumber to start as number 1. This does that:

    $commentNumber = 1;

    Pretty simple. The second bit of code you have sets $commentNumber to 1 in the comment loop, at least in the first iteration of the loop (it increments the value by 1 for each comment that would exist).

    So, what are you looking to accomplish here?

    Umm, here is a newb question: where does this code go? In the index file or in the comments file or where?

    Hi,

    Kafkaesqui, I simply want to number comments like here:

    https://www.xeenat.com/2006/09/05/and-so-it-begins/#comments

    So the first comment gets a 1, the second gets a 2, the third gets a 3, etc.

    With my second code it prints all the numbers together. Instead of just one number per comment.

    Hope that helps… Thanks!

    Thanks,

    Will

    carnold, it would go into the Comments template, i.e. comments.php.

    war59312, assuming you’re merely duplicating the code you show above, it prints out that way because you are running through a comments loop but not displaying comments (or in fact anything but the numbers).

    Normally one would stick $commentNumber++; at the top of the ‘normal’ comments loop, and then strategically echo it before (or within, or after, depending on your layout) the comment is displayed.

    If you continue to have problems, you can place the source of your comments.php here:

    https://wordpress.pastebin.ca/

    and if you reply back with the url to it, we can provide suggestions on changing the code to display the comment number ‘correctly.’

    war59312, as I said before you’re running a comments loop and *just* echoing the comment number for each. This is a skeletal example of a comments loop:

    <?php foreach ($comments as $comment) : ?>
    ~
    Template tags and whatnot for each comment would go here.
    ~

    <?php endforeach; ?>

    What you’ve done is nest a comments loop inside the original one of your comments template (at least I suspect you have; as you didn’t provide your entire comments.php I’ve no idea what else is going on in it). In other words, for each comment you display you’re running through the whole comments loop again!

    Here’s how your code fragment *should* look:

    <p class="comment-number"><a href="#comment-<?php comment_ID() ?>" title="<?php comment_date('F jS, Y') ?> @ <?php comment_time() ?>"><?php $commentNumber++; echo $commentNumber; ?></a>

    A comment will already be in a comments loop; you don’t need to recreate that loop to run your value iteration of $commentNumber, etc.

    oh ok yeah was wondering why it was repeating…yeah thanks buddy…

    working perfectly now…

    thanks for your patience and help!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Comment Number Display’ is closed to new replies.