What is the best way to count and display the number of posts?

I have a section in my site where I display the 10 most popular posts. Now, I want to display a number, from 1 to 10, besides each post. Like this:

Top 10 posts

Read More
  1. post a
  2. post b
  3. post c

and so on…

I implemented a counter inside the while loop. I initialized the counter at 0 $counter = 0; and then incremented it by one <?php echo ++$counter; ?>

My concern is, due to my site receiving thousands of visits a day, I want to know if this method will consume my site’s resources considerably. Is there a better way to do this? Maybe to implement some sort of caching?

What advice can you give me?

Related posts

1 comment

  1. Another solution would be to use an ordered list.

    $my_query = new WP_Query($args);
    if ( $my_query->have_posts() ) :
        echo '<ol style="list-style:decimal">';
        while( $my_query->have_posts() ) : $my_query->the_post();
          echo '<li><a href="' . get_permalink( get_the_ID() ) . '">' . get_the_title() . '</a></li>';
        endwhile;
        echo '</ol>';
    endif;
    

Comments are closed.