• My apologies if this has been answered anywhere already- if it has, I couldn’t find it.

    I am using WP to run a regular blog and a separate ComicPress installation. I am creating a standard kind of webcomic navigation (First/ Previous/ Next/ Latest), using graphics that will change depending on whether or not there is a next or previous page to go to. For example, if I have 10 posts and the reader is on page 5, their nav will look like this:

    [<<] [<] [>] [>>]

    But if they are on post 10 of 10, it will be:

    [<<] [<] > >>

    …meaning that the last 2 buttons are disabled because there is nowhere further to go. I plan to determine this by comparing the total # of posts with the post number. I have found a way to obtain the total # of posts in a category:

    <?php $cat_count = $wpdb->get_var(“SELECT category_count from $wpdb->categories WHERE cat_ID = ‘2’”); ?>
    <?php echo $cat_count ?>

    But I cannot find a simple way to return a simple ordinal number for a given post. All I can get is the post ID, which as you know, could well be greater than the actual post number.

    Is there any way to figure out a post number (ordered by date)? Or should I be taking another approach entirely to this and forget those scripts I used to hack together in Javascript? I am a complete PHP noob so any help would be appreciated.

    To see what I am talking about, you can go to https://www.entropiacomics.com

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Hmmm.. I’m confused.

    Do you want to get the Nth post in a given category? If so, you could use the query_posts or WP_Query class to get posts. It’s not immediately obvious how to get post N, but you’d use these options in your query string:
    cat=X – Specify which category you’re looking at, by number.
    posts_per_page=1 – Forces it to only get one post per “page” of posts.
    paged=N – Get page N of those posts.
    Thus you’d get the Nth post in that category.

    On the other hand, if you have a post_id already and just want to find which post number it is in a given category, that’s a lot trickier.

    Thread Starter superiorstudio

    (@superiorstudio)

    Yes, it is the trickier one that I want. ?? I can get a post_id no problem.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    The only way I can think to do it would be to:
    -Select a list of all the post_id’s in a category, in the correct order
    -Loop through them all looking for your post_id
    -Keep a counter and when you find it, there’s your number.

    There’s really no other way. Relational databases have no inherent order to them…

    Thread Starter superiorstudio

    (@superiorstudio)

    Yes, I see what you mean. I will give that some thought, thanks!

    I’m still trying to find something that will do this for me, and I’m a complete PHP n00b.. ??

    WP already does most of what you need in “browse a single category” mode and “previous posts – next posts” works, just make it display a single post per page. Or use get_posts('arguments') and include an increment in the foreach(). That will let you assign numbers to all posts in a single category and probably make a menu like: << < 1 2 3 4 5 > >> In any case, have a look in $WP_Query (print_r($WP_Query)) as Otto42 suggested, to see what’s going on in the background.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Finding ordinal post (not ID) number?’ is closed to new replies.