ulman
ulman

Reputation: 13

How to put numbers (just the order of posts like 1,2,3...) for my posts in my WordPress content page?

I have a home page which loop posts here like:

if (have_posts()) :

    while ( have_posts() ) {          

        the_post();            
        get_template_part('content', get_post_format());

    }  

In my content page there, I display 25 posts in a page.

My requirement is that, I need numbers for each post like their order (1,2,3...).

How do I implement this?

Upvotes: 0

Views: 125

Answers (2)

d79
d79

Reputation: 3848

You can use the property $current_post of WP_Query:

global $wp_query;

while ( have_posts() ) {          

    the_post();
    get_template_part('content', get_post_format());      

    echo $wp_query->current_post + 1;

}  

It starts from 0, therefore the + 1;

Upvotes: 1

Jonathan
Jonathan

Reputation: 6537

Could you simply add a counter that increments each time you run through the while loop?

// Initialize the counter at 1.
$counter = 1;

while ( have_posts() ) {          

  the_post();
  get_template_part('content', get_post_format());      

  // Display the current post number under the post content:
  echo "Post number " . $counter;

  // Now increase the counter by 1:
  $counter++;

}  

Upvotes: 0

Related Questions