Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

  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?

share|improve this question

1 Answer 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;
share|improve this answer
    
+1 from me. I always write php and forgot that html exists :) –  G. M. Sep 7 '13 at 8:43
    
I tried your solution, however, the numbers are not displaying. –  Gixty Sep 7 '13 at 8:44
    
@Gixty is a css problem, I think. –  G. M. Sep 7 '13 at 8:46
    
@Gixty added style="list-style:decimal" to force number showing –  G. M. Sep 7 '13 at 8:49
    
@Horttcore: Thank you!! yes it is, i found this: stackoverflow.com/questions/4643913/… –  Gixty Sep 7 '13 at 8:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.