Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

hi im trying to output pagination without any success. below is my code in a page-products.php template

<?php $products_query = new WP_Query(array(
    'post_type' => 'product',
    'posts_per_page' => 4,
    'paged' => (get_query_var('page') ? get_query_var('page') : 1),
));?>

<?php if($products_query->have_posts()) : while($products_query->have_posts()) : $products_query->the_post();?>

<?php the_content; ?>

<?php endwhile; ?>
<?php posts_nav_link();?>
<?php endif; ?>

i cant seem to output my pagination links. can anybody help spot whats wrong?

share|improve this question
is posts_nav_link() only for paginating the default 'post' post type?. i tried using echo paginate_links() with no success also. – George Ananda Eman Oct 3 '12 at 18:15

1 Answer

I believe the query var that you are retrieving should be paged and not page. See the codex and adjust your query to the following:

$products_query = new WP_Query(array(
    'post_type' => 'product',
    'posts_per_page' => 4,
    'paged' => (get_query_var('paged') ? get_query_var('paged') : 1),
));

However, if this is an archive for your product post type why not use archive-product.php and let WP handle the query and pagination automatically? See template hierarchy.

share|improve this answer
actually ive been wanting to do that, how do i access the page without giving it a page template name and making a page and then selecting it as a template? i prefer my templates to be called without assigning it manually. i also tried that code on a taxonomy-collection.php template, would that be the same thing? – George Ananda Eman Oct 3 '12 at 18:44
That is a totally different question, but basically you'd tell your custom post type to have an archive: codex.wordpress.org/Function_Reference/… and you can change the URL via the rewrite parameter – helgatheviking Oct 3 '12 at 20:20

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.