Support » Themes and Templates » Is there any way to find out the number of an article?

  • Hi,
    I have a problem concerning the IDs of the articles.
    I want to know what number an article is, so I can write
    “Post 5: Title of article..”

    You migh know that in the wordpress DB every article has its own ID but also the copies and backups of an article are numbered with the same IDs.
    So you have maybe 5 published articles, but actually you already used the IDs 1-30 because WP made backups or saved every different version of your posts.. you know what I mean?

    So my problem is now, that I need to know what number a post is.

    But how can I find this out?

    I can’t unse the IDs because of what I wrote above.

    Here’s my (WIP) design:
    I need it for the speechbubbles.

    Would be really great if anyone could help me, I have no idea how to geht those numbers.

Viewing 11 replies - 1 through 11 (of 11 total)
  • $post->ID ?

    Well, no that’s exactly what I can unfortunately not use because of what I described above 😉
    See, let’s say I have 2 posts, but each post was edited 3 times, now in the DB it looks like this:

    ID | post_title | post_status
    1 | bla | publish
    2 | bla | refurbish
    3 | bla | refurbish
    4 | lorem | publish
    5 | lorem| refurbish
    6 | lorem| refurbish

    You see, my two posts have the IDs 1 and 4 because of those backups hat use the same IDs, while i would need them to have he IDs 1 and 2.

    I am not sure whether there are any functions for that.
    But simple php code like the following will work.

    Put <?php $i=1; ?> before the loop in index.php.

    Inside the loop, put

    echo $i; ?>

    ahh you want sequential numbering not related to actual post ids. My apologies, Gisha is correct for listing purposes within the loop.

    But I’m not sure that is what you want either, for the number to display on individual posts it’ll need something a little more complex.



    Forum Moderator

    here is something i prepared earlier …

    @alchymyth nice!

    First of all thank you guys for all your answers!

    @Gisha James
    That’s exactly what I use in the loop 🙂 but when it comes to displaying the numer of only one post Rich ‘elfin’ Pedley is right, I need something different. But thanks!

    Once again, you are my hero!
    I’ll try that right!

    Unfortunately it doesn’t work as hoped.
    There is no output from the function.

    I put this into my fuctions.php in my theme

    function updateNumbers() {
        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' );

    And this is my code for displaying the post:

    global $post;
    $myposts = get_posts('numberposts=1&offset=0&category=3');
    foreach($myposts as $post) : setup_postdata($post);?>
    //then some output like the_content()...
    //and herer the call to get the number: but this doesn's show anything:
    <?php echo get_post_meta($post->ID,'incr_number',true); ?>
    <?php endforeach; ?>

    Ok now THIS really creapes me out, all of a sudden it works. I changed absolutely nothing, just reloaded half an our later and it worked.
    You might say, cache but I also cleared the cache before and it didn’t help. But now it works. No Idea why, but who cares 😀

    Thanks a lot all of you and especially alchymyth!



    Forum Moderator

    I changed absolutely nothing, just reloaded half an our later and it worked.

    as is mentioned in my post, the function is only triggerd when you edit/delete/create a post.

    when you add the function to the functions.php, until you do some of the above, the custom fields will not get generatd and filled; same with post previews – the field will be empty until you save the post.

    a bit confusing, i admit.

    Ahh right, I forgot bout this! Thanks again!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Is there any way to find out the number of an article?’ is closed to new replies.