Displaying post order number in archives
The article begins with a practical scenario: a website administrator needs to display podcast episode numbers in archives without manually adding metadata to 500+ existing posts.
The tutorial covers:
- Obtaining total counts of published posts by type
- Implementing backward counting through WordPress loops
- Adjusting numbering across paginated archives
- Integrating functionality with Jetpack Infinite Scroll
Getting Started Requirements:
- A WordPress installation (preferably local)
- A custom post type with ‘podcast’ slug and archive support enabled
- Multiple posts in various statuses
- Jetpack plugin with Infinite Scroll capability
Deep Dive: wp_count_posts Function
Getting post counts regardless of status:
$post_count = wp_count_posts();
echo "<p>Total number of posts (all post statuses): $post_count</p>";
Counting specific post types:
$post_count_alternative = wp_count_posts( 'post' );
echo "<p>Total number of posts: $post_count</p>";
Published posts only:
$post_count_published = wp_count_posts()->publish;
echo "<p>Total number of posts (just published): $post_count_published</p>";
Published podcasts specifically:
$podcast_count = wp_count_posts( 'podcast' )->publish;
echo "<p>Total number of podcasts (just published): $podcast_count</p>";
Counting Backwards Through Loops
Archive template implementation:
// Get total count of published podcasts
$podcast_count = wp_count_posts( 'podcast' )->publish;
// Start loop
while ( have_posts() ) : the_post();
// Use include( locate_template() ) so we can pass variable values to template part
include ( locate_template( 'template-parts/content-podcast.php' ) );
// Decrease counter
$podcast_count--;
// End loop
endwhile;
The article emphasizes using include ( locate_template() ) rather than get_template_part() because the latter doesn’t support variable passing.
Content template part:
<article class="hentry" data-count="<?php echo $podcast_count; ?>">
<?php the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' ); ?>
</article><!-- .hentry -->
CSS display:
.podcast-list .hentry:after {
content: "Episode #: " attr(data-count);
}
Handling Pagination
Getting current page number:
// Get page number
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
Retrieving posts per page:
// Get posts per page option
$posts_per_page = get_option( 'posts_per_page' );
Pagination math:
Adjusted podcast count = Published podcast count - ( ( Current page number - 1 ) * Posts per page )
Example: With 100 total podcasts, 10 per page, on page 2, the first episode should be #90.
Complete implementation:
// Get total count of podcasts
$podcast_count = wp_count_posts( 'podcast' )->publish;
// Get page number minus 1
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$paged = $paged - 1;
// Get posts per page option
$posts_per_page = get_option( 'posts_per_page' );
// Adjusted podcast count
$podcast_count = $podcast_count - ( $paged * $posts_per_page );
Jetpack Infinite Scroll Integration
Theme support declaration:
function themetry_jetpack_setup() {
// Add theme support for Infinite Scroll.
add_theme_support( 'infinite-scroll', array(
'container' => 'entries-list',
'render' => 'themetry_infinite_scroll_render',
'footer' => 'page',
'wrapper' => false,
) );
}
add_action( 'after_setup_theme', 'themetry_jetpack_setup' );
Render function:
function themetry_infinite_scroll_render( $args ) {
// Get total count of posts
$podcast_count = wp_count_posts( 'podcast' )->publish;
// Get posts per page option
$posts_per_page = get_option( 'posts_per_page' );
// Get page number
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// New post count = total post count - ( page number * posts per page )
$podcast_count = $podcast_count - ( $paged * $posts_per_page );
while ( have_posts() ) {
the_post();
if ( is_post_type_archive( 'podcast' ) {
// Must use locate_template so we can pass $podcast_count value
include ( locate_template( 'template-parts/content-podcast.php' ) );
// Decrease counter
$podcast_count--;
} else {
get_template_part( 'template-parts/content' );
}
}
}
The tutorial notes that unlike standard pagination code, the Infinite Scroll render function does not subtract 1 from $paged because it references the previous page number.
The author emphasizes this technique works best for custom post types and cautions against broad implementation across entire blogs, warning it could confuse users seeing different numbering in different views.