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.

For a post x in a category y, how can the following output be achieved:

  • Post x is the nth out of m posts in the category y.

where m is the total number of posts in the category, and n is the number of the current post (ordered by publishing date).

share|improve this question

1 Answer 1

Are you using the main query or a custom WP_Query for displaying the posts?

If so :

 $wp_query->found_posts; //Main query
 $wp_query->current_post; // current post index

or

 $my_wp_query->found_posts; //custom wp_query object - total found posts
 $my_wp_query->current_post; // current post index

So, example

 if( have_posts() ) : while( have_posts() ) : the_post();
 ?>
      <p>The post <?php the_title(); ?> is <?php echo $wp_query->current_post; ?>th of <?php echo $wp_query->found_posts; ?> in category <?php echo 'your cat here'; ?></p>

 <?php endwhile; endif; ?>
share|improve this answer
3  
Instead of hardcoding the "th", here's how to use the proper ordinal suffix: stackoverflow.com/questions/3109978/… –  Geert Feb 1 '12 at 15:37
    
@Geert +1 - I only give rough examples, but that link will surely come in handy. –  Shane Feb 1 '12 at 15:42
    
It's for a single post page. –  Ben Feb 1 '12 at 22:03

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.