• Hi,

    im sorting my posts in a certain way (by rating) what i need to do is to fetch the number they have according their position on the list. For example, the first post should be 1, the second 2 and so on. I wonder if its possible and how.

    Thank you.

Viewing 14 replies - 1 through 14 (of 14 total)
  • I believe that you just need to add a counter in your Loop and increment it for every post.

    Thread Starter destrones

    (@destrones)

    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.

    Thread Starter destrones

    (@destrones)

    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;
    Thread Starter destrones

    (@destrones)

    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;
    Thread Starter destrones

    (@destrones)

    Yes, $paged is zero. Wehre should i put that formula? before the loop starts or inside the loop?

    Where you had $i = 1 before.

    Thread Starter destrones

    (@destrones)

    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++; ?>

    Yes.

    Thread Starter destrones

    (@destrones)

    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.

    Thread Starter destrones

    (@destrones)

    Nevermind again. I had fucked up. Your code actually works, thank you!

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Ger posts sorting order number’ is closed to new replies.