Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On my page I've got a query that retrieves posts with specific parameters and I'm displaying one post per page with pagination. I've figured out how to get the total count of posts found by the query. I want to add some navigation that essentially says "You're looking at 'n' of 25 posts" and have 'n' increase or decrease as the user clicks through the 'Next Post/Previous Post' links.

Is there a function already in place for this?

Here's my query and how I'm outputting it to the page:

<?php
        $display_count = 1;
        $page = get_query_var('paged') ? get_query_var('paged') : 1;
        $offset = ($page -1)*$display_count;
        $query_args = array(
            'post_type' => 'post',
            'orderBy' => 'date',
            'order' => 'desc',
            'number' => $display_count,
            'posts_per_page' => 1,
            'page' => $page,
            'offset' => $offset
            );
        ?>
<article id="portfolio_item">
      <?php $portfolio = new WP_Query($query_args); ?>
      <!-- Total number of posts -->
      <p> <?php echo "There are ".$portfolio->found_posts." Planning Projects"; ?></p>
      <!-- Pagination Links -->
            <p> 
                <?php previous_posts_link( '<< Previous Posts | ', $portfolio->max_num_pages ); ?>
                <?php next_posts_link( 'Next Posts >>', $portfolio->max_num_pages ); ?>
      </p>
        <?php if( $portfolio->have_posts() ) : while( $portfolio->have_posts()) : $portfolio->the_post(); ?>
      <h2><?php the_title() ?><span id="project_location"> <?php echo get_post_meta($post->ID, 'project_location', true) ?></span> <span id="project_class">{ <?php echo get_post_meta($post->ID, 'project_services', true) ?> }</span></h2>
        <?php the_content() ?>
        <?php endwhile; endif; ?>
        <?php wp_reset_query(); ?>
      </article>
    </section>
share|improve this question
2  
You mean "Page $page of $portfolio->found_posts."? –  toscho Mar 21 '13 at 19:42
    
Exactly, That's what I'm looking for! –  Ofeargall Mar 21 '13 at 19:48
    
Worked like a charm. Can you make an answer so I can give credit where awesome credit is due? Thank you! –  Ofeargall Mar 21 '13 at 19:50
    
I do that right now. :) –  toscho Mar 21 '13 at 19:52
add comment

1 Answer

up vote 1 down vote accepted

You have all the necessary variables already at hand: $page is the current page number, and $portfolio->found_posts the total number of posts.

Both should be integers, but to be sure and to keep the code translatable I would use something like this:

$current = sprintf(
    _x( 
        'Page %1$d of %2$d.',  // default string
        '1 = current page, 2 = total pages', // context information for translators
        'your_text_domain'
    )
);

echo '<p>' . esc_html( $current ) . '</p>';

So nothing dangerous can happen, even if a translator injects some HTML.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.