I believe that you just need to add a counter in your Loop and increment it for every post.
Thats a good idea. I would appreciate if you have a suggestion on how to do it.
Thank you.
The exact instructions depend on your theme and the template that is displaying the posts. In general, your code for incrementing the variable might look something like this:
if ( have_posts() ) : while ( have_posts() ) : the_post();
++$post_counter;
// more of the loop code here
echo "<p>Counter: $post_counter</p>";
// even more of the loop code
endwhile; endif;
Of course, you would need to echo or otherwise display the $post_counter where you wanted it, not as I have shown above.
Nevermind, i used <?php $i = 1; while (have_posts()) : the_post(); ?> and then <?php echo $i++; ?> which worked. However, it only looks fine on the first page since the counter shows the order of the posts FOR the current page and not overall. That means all the pages have a post number 1, 2, 3 and so on so i think the counter on the loop is not gonna work. Any suggestions?
You can set the initial value of the counter using the posts per page and the current page number. Assume the posts_per_page is in $posts_per_page and the current page number is in $paged. The formula for the initial value is:
$i = ($paged - 1) * $posts_per_page +1;
That looks good. Is $paged and $posts_per_page are already default WP variables? or do i still need to define them myself?
Both ‘should be’ global variables, but $paged might be zero, so you have to check for that.
Here is a modified formula that should work:
$i = ( max($paged,1) - 1 ) * $posts_per_page + 1;
Yes, $paged is zero. Wehre should i put that formula? before the loop starts or inside the loop?
Where you had $i = 1 before.
Right now i have it like this
<?php $i=1; while (have_posts()) : the_post(); ?>
<?php echo $i++; ?>
so if i use your formula it should be like this?
<?php $i = ( max($paged,1) – 1 ) * $posts_per_page + 1; while (have_posts()) : the_post(); ?>
<?php echo $i++; ?>
I tried but didnt work. However this did the trick for the page 2 and on:
<?php $i=1; while (have_posts()) : the_post(); ?>
<?php $rank=$i++; echo $rank + (($paged – 1) * $posts_per_page); ?>
but since $paged is 0 when on the first page, it doesnt work for page 1. But i think we are close.
Nevermind again. I had fucked up. Your code actually works, thank you!