• 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.

Viewing 9 replies - 1 through 9 (of 9 total)
  • 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?

    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):
    setup_postdata($post);
    $counts++;
    add_post_meta($post->ID, 'incr_number', $counts, true);
    update_post_meta($post->ID, 'incr_number', $counts);
    endforeach;
    endif;
    }  
    
    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); ?>

    Thread Starter jimmiejo

    (@jimmiejo)

    Thanks, alchymyth. I’ll give it a try shortly and resolve this thread if it works like a charm ??

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

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

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

    LazyDev

    (@milber)

    [solved]

    LazyDev

    (@milber)

    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) :
                                   setup_postdata($totalpost);
                                    $counts++;
                                    add_post_meta($totalpost->ID, 'incr_number', $counts, true);
                                    update_post_meta($totalpost->ID, 'incr_number', $counts);
    
                                   endforeach;
                               endif;
    
                               wp_reset_query();
    
                            }
    Michael

    (@alchymyth)

    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.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Displaying an incrementing number next to each published post’ is closed to new replies.