0

I have a custom post type called Projects.

When viewing a single 'project' post I have next/prev buttons to navigate through them.

I also want to display text saying "Project 2 of 4" or ideally pagination icons.

This is easy on index.php but I can't work out how to do this on a single post page.

Thanks

1 Answer 1

1

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.

5
  • thanks milo - i'll give this a go. And how about if i wanted to use pagination buttons like this - design.sparklette.net/teaches/…
    – fxfuture
    Oct 9, 2011 at 5:33
  • you would get the links to each post via the_permalink(), and the numbers for each button from $all_projects->current_post.
    – Milo
    Oct 9, 2011 at 7:16
  • Thank you so much. This was EXACTLY what I was looking for!
    – fxfuture
    Oct 10, 2011 at 2:55
  • Hi Milo, I've just found this is causing a problem displaying the_content(); On my local test server it displays the content from the first post on all posts, and on my web server it doesn't display the content at all. Any ideas how to fix this?
    – fxfuture
    Oct 11, 2011 at 21:11
  • I put my code in a new question here - wordpress.stackexchange.com/questions/30841/…
    – fxfuture
    Oct 11, 2011 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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