1

I have my own custom post type, and 15 items there.

This code shows all the items:

<?php   $loop = new WP_Query( array( 'post_type' => 'my_post_type', 'posts_per_page' => 15 ) );
  while ( $loop->have_posts() ) : $loop->the_post(); ?>
(...)

When I change posts_per_page to "5" there should be 3 pages, but it displays only 5 items and there are no other pages (or at least I don't know how to access them).

This code displays nothing:

<?php if (  $wp_query->max_num_pages > 1 ) : ?>
  {menu or any html code}
<?php endif; ?>

Maybe I do something wrong? I have the whole code/loop in page-myposttype.php file.

I've tried this code:

   $temp = $wp_query;
    $loop= null;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $loop = new WP_Query( array( 
    'post_type' => 'my_post_type',   
    'paged' => $paged, 
    'posts_per_page' => 2 ) );
        while ( $loop->have_posts() ) : $loop->the_post(); ?>

But no luck... When I type wordpress/my_post_type/page/2 (page/3 etc.) manually I see other pages and they work & look fine. But I'm unable to echo the default navi... :(

2
  • where is your code to print out the next/previus page link?
    – Bainternet
    Feb 2, 2011 at 0:39
  • Your query uses $loop, but you check the number of pages of $wp_query. $wp_query will contain the default query, which is probably a page in your case?
    – Jan Fabry
    Feb 2, 2011 at 8:54

1 Answer 1

1

Answered a question that's virtually identical to this very recently on the WordPress.org forums.

Custom Post Type Archive Page - Pagination Isn't Working

I've provided a work-around / fix in that thread that should also work for you to.

:)

EDIT: You also need to add the paging parameter to your query..

Eg.

$args = array( 
    'post_type' => 'my_post_type', 
    'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
    'posts_per_page' => 15, 
);
$loop = new WP_Query;
$loop->query( $args );

I reformatted the code to make it a little more readable, end result will be the same.

5
  • I'm using wp_pagenavi and it doesn't help me at all :( Eeven with your code when I click Next/Prev Entry - I'm getting the same items all over again (for example if there are 6 items and I display 2, I'm getting 3 pages of items number 1 and 2). Feb 2, 2011 at 13:50
  • That's because you're missing the paging parameter in your query, i'll add it into my answer.
    – t31os
    Feb 2, 2011 at 13:53
  • your answer was very helpful and works fine but I BADLY need to display the default Wordpress pagination/page navigation. Do you have any ideas how to achieve that? I'm using wp_pagenavi plugin based on the default WP navigation and it doesn't "work" with your code. Feb 3, 2011 at 18:40
  • nvm, figured it out (had to put pagenavi code instead of default links), thanks a lot! Feb 3, 2011 at 18:52
  • Glad you got it figured out and i'm happy to have helped... :)
    – t31os
    Feb 3, 2011 at 18:54

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.