WordPress.org

Ready to get started?Download WordPress

Forums

Is there any way to find out the number of an article? (12 posts)

  1. hoktar
    Member
    Posted 3 years ago #

    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: blog.stephanskorepa.net
    I need it for the speechbubbles.

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

  2. elfin
    Moderator
    Posted 3 years ago #

    $post->ID ?

  3. hoktar
    Member
    Posted 3 years ago #

    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.

  4. Gisha James
    Member
    Posted 3 years ago #

    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

    <?php
    $i=$i+1;
    echo $i; ?>

  5. elfin
    Moderator
    Posted 3 years ago #

    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.

  6. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    here is something i prepared earlier ...

  7. elfin
    Moderator
    Posted 3 years ago #

    @alchymyth nice!

  8. hoktar
    Member
    Posted 3 years ago #

    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!

    @alchymyth
    Once again, you are my hero!
    I'll try that right!

  9. hoktar
    Member
    Posted 3 years ago #

    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):
        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' );
    ]

    And this is my code for displaying the post:

    <?php
    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; ?>
  10. hoktar
    Member
    Posted 3 years ago #

    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 :D

    Thanks a lot all of you and especially alchymyth!

  11. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    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.

  12. hoktar
    Member
    Posted 3 years ago #

    Ahh right, I forgot bout this! Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.