Ready to get started?Download WordPress


Displaying an incrementing number next to each published post (10 posts)

  1. jimmiejo
    Posted 4 years ago #

    I'm trying to figure out how to display an incrementing number next to each published post = Article #1, Article #2, Article #3, etc...

    The post_ID isn't progressive, so that won't do the trick.

    Was wondering if anyone has ever needed such a function or can lead me in the right direction? Thanks.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    Would that be simply numbers that increment per archive listing? If yes, why not add a simple counter, set it to zero before the Loop, increment it immediately after the Loop starts and then display it on the page as required?

  3. this is how i implemented it into my blog a few months ago; first time, it will automatically create a custom field 'incr_number' for every published post, and update the value every time any post is deleted, updated, or published.

    add this snippet to functions.php of your theme:

    function updateNumbers() {
    /* numbering the published posts: preparation: create an array with the ID in sequence of publication date, /
    / save the number in custom field 'incr_number' of post with ID  /
    / to show in post (within the loop) use <?php echo get_post_meta($post->ID,'incr_number',true); ?>
    / alchymyth 2010 */
    global $wpdb;
    $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    $counts = 0 ;
    if ($pageposts):
    foreach ($pageposts as $post):
    add_post_meta($post->ID, 'incr_number', $counts, true);
    update_post_meta($post->ID, 'incr_number', $counts);
    add_action ( 'publish_post', 'updateNumbers' );
    add_action ( 'deleted_post', 'updateNumbers' );
    add_action ( 'edit_post', 'updateNumbers' );

    to show it next to your post, use:
    <?php echo get_post_meta($post->ID,'incr_number',true); ?>

  4. jimmiejo
    Posted 4 years ago #

    Thanks, alchymyth. I'll give it a try shortly and resolve this thread if it works like a charm :)

  5. wesbos
    Posted 4 years ago #

    Amazing! Thanks so much. Exactly what I was looking for.

  6. razn8
    Posted 4 years ago #

    This would be great, but it doesn't appear to work in WordPress 3.0.1. Any ideas?

  7. continuos post numbers - it only activates when you create/edit/delete a post for the first time after adding the code.

  8. Milber
    Posted 4 years ago #


  9. Milber
    Posted 4 years ago #

    Ok, not solved after all ;-) : Strangely enough, the function only counts up to 5 on my installation, then starting again at 1. I'm dumbstruck by this, pls help.

    function updateNumbers() { 
                            $totalposts = get_posts('category_name=folio&post_type=post&posts_per_page=-1&orderby=date&order=DESC');
                           $counts = 0;
                           if ($totalposts) :
                               foreach ($totalposts as $totalpost) :
                                    add_post_meta($totalpost->ID, 'incr_number', $counts, true);
                                    update_post_meta($totalpost->ID, 'incr_number', $counts);
  10. this has to do with the 'posts_per_page' parameter
    - even if the codex states that 'get_posts()' takes the same parameters as query_posts()
    - 'posts_per_page' is the one parameter that does not work -

    use 'numberposts' instead.

Topic Closed

This topic has been closed to new replies.

About this Topic