Support » Fixing WordPress » Get pount position in total post count

  • Resolved Doodlebee


    Just wondering if anyone knew how to grab the post position within a total post count?

    That probably doesn’t make much sense LOL

    Basically, I’m trying to do *almost* what the WP Page-Navi plugin does. (That sucker comes in very close!) I’m setting up the blog to display one post at a time. At the bottom, where the “Next Previous Post” links are, instead I want « Previous Posts (X of Y). “X” would be the current post and “Y” the total count of published posts for the site. So if we’re on the most recent post out of 264 posts on the site, the index page would display “« Previous Posts (1 of 264)”

    Now, I’ve had no problem getting the *total* count – that’s dead easy.

    $numposts = wp_count_posts(‘post’);
    echo $numposts->publish;

    That gives me the total number of posts that have been published on the site. I’m just having a time trying to figure out how to get the *current* post’s position in the count. I’m thinking I could somehow grab what page number I’m on, since I’m only showing one single post at a time – but if I did that thenI might as well use the WP Page-Navi plugin. The problem with *that* is that the formatting/layout of the WP Page-Navi plugin isn’t correct, and it just seems like an awful lot of code for one single thing. (seems I could write a simple unction for this, rather than use a plugin – I don’t know, maybe I’m overthinking this.)

    Anyone feel like kicking me in the teeth with an idea? (even if it is “shut up and use Page-Navi” – of course then I’ll have to try and figure out how to alter the output without altering the core files)

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try something like $tot_pages = $wp_query->max_num_pages;

    I’ve used that in search.php to successfully show the total number of search results returned.

    Using the page number seems like a sensible approach to me, and you can simplify the total count of posts.

    If you use this instead.

    $total_posts = (int) $wp_query->found_posts;

    You’ll avoid the need to do a lookup query(eg. wp_count_posts) to get the total count.

    For the current post, just use something simple like..

    $current_page = ( get_query_var('paged') && get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;

    Then wherever you want the x out of y message, do something like…

    <?php echo '('.$current_page.' of '.$total_posts.')'; ?>

    EDIT: Or what esmi suggested (didn’t think to refresh the page before posting).

    Thank you both! I don’t know how this got marked “resolved” before today – ’cause I just saw your responses today – but it’s resolved now! I used both of your suggestions, messed with it a bit, and came up with the solution (thank you SO much for pointing me in the right direction!)

    t31os_, yous gave the the *current* page I was on, but I needed one page up and one below. So my code now looks like this:

    <?php if(!is_page()) {
    	  	$thispage = get_query_var('paged');
    	  	if($thispage == '0') $thispage = 1; // because the most recent post is given a value of "0", we need to reset it to "1"
    	  	    $next_page = $thispage - 1;
    	  	    $prev_page = $thispage + 1;
    	  	    $tot_pages = $wp_query->max_num_pages;?>
    	  <p class="postmetadata clear">
    	    <span class="left"><?php next_posts_link('&laquo; Previous Post (' . $prev_page . ' of ' . $tot_pages . ')') ?></span>
            <span class="right"><?php previous_posts_link('(' . $next_page . ' of ' . $tot_pages . ') Next Post &raquo;') ?></span>
    	  <?php } ?>

    Thanks again!

    I should note – the above does work, but only if you have a setup like mine (where it uses one post per page). It does *not* work on single.php – because it’s not paginated 😉 I still have to figure something out for that – but for now I’m letting it go.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get pount position in total post count’ is closed to new replies.