Display “Post 2 of 4” on single post page?

You could create a new query for all posts in your custom post type, then iterate through each and match it to the current post id to figure out its position.

// save current id to match later
$current_project_id = $post->ID;

$args = array(
    'post_type' => 'project',
    'posts_per_page' => -1
);
$all_projects = new WP_Query( $args );

while( $all_projects->have_posts() ) : $all_projects->the_post();

    // simple 'if' test to find the current post in the loop
    // note that current_post starts at zero, which is why we add 1 to it.
    // you can also output the_title, the_permalink, thumbnails, etc..

    if( $post->ID == $current_project_id ):
        echo "project " . ( $all_projects->current_post + 1 ) . " of " . $all_projects->post_count;
    endif;

endwhile;

The caveat here is that you’re loading all of your projects on every page, which could get expensive if you’ve got a lot of projects. You’ll probably want to set up some caching to keep things speedy anyway.

Leave a Comment