Ever wanted to show each comment’s position in a list without using an ordered list (<ol>) to do it? Unfortunately, WordPress doesn’t include a function do do this. However, by using a simple PHP trick, we can accomplish this.

Open comments.php and scroll down to where the comments loop begins:

&amp;lt;?php foreach ($comments as $comment) : ?&amp;gt;

Before the loop begins, we need to set the PHP variable we’ll use to determine the number. Change the above line to the following:

&amp;lt;?php $commentNum = 1; foreach ($comments as $comment) : ?&amp;gt;

We’ve now assigned the value “1″ to the variable $commentNum.

Next, find the end of the loop:

&amp;lt;?php endforeach; ?&amp;gt;

We need to increase the value of $commentNum by 1 before the loop ends. To do this, we use PHP’s increment operator:

<?php $commentNum++; endforeach; ?>

Now you can use the variable $commentNum anywhere within the loop. Simply echo the variable with PHP to display the current comment’s number.

&amp;lt;?php echo $commentNum; ?&amp;gt;

Republished by Blog Post Promoter


Related posts