0

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).

2

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; ?>
3
  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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