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.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 '11 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 '11 at 7:16
    
Thank you so much. This was EXACTLY what I was looking for! –  fxfuture Oct 10 '11 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 '11 at 21:11
    
I put my code in a new question here - wordpress.stackexchange.com/questions/30841/… –  fxfuture Oct 11 '11 at 22:02

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.