• Resolved fukr

    (@fukr)


    I am currently developing a theme and need to sort each post with a numeber(not post ID).
    for example: there are 10 posts in frontpage, then the first post get an attribute like “article-1”, the second “article-2”, the third article “article-3″… likewise. when we click to page2, the first post in page2 also gets a attribute “article-1″… that means in each page the post number still from 1, 2, 3…

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try something like:

    <?php if (have_posts()) : $c = 0;
    $new_class = 'article';
    while (have_posts()) : the_post();
    $c++?;
    $new_class .= '-' . $c;>
    <div <?php post_class($new_class); ?>>
    <!-- do stuff ... -->
    </div>
    <?php endwhile; ?>
    <?php endif; ?>
    Thread Starter fukr

    (@fukr)

    thanks. i tried your code but didn’t work.
    I changed a little like below:

    <?php if (have_posts()) : $count = 0; ?>
      <?php while (have_posts()) : the_post(); $count++; ?>
      <article id="post-<?php the_ID(); ?>" class="article-<?php echo $count; ?>">
      <!-- codes ... -->
      </article>

    when i checked the frontend, only show class=”article-“, no number displayed.

    can you please help?

    On the face of it, I can’t see any obvious reason why your code should not work. Or mine, for that matter…

    Thread Starter fukr

    (@fukr)

    it works now, my mistake…

    thanks, you’re very helpful

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

The topic ‘How to add a numeric sorting for each post’ is closed to new replies.