Hi guys, I've been looking all over for a solution to this but yet to find something I can get to work!
I have set up a page template with a custom loop to show my post thumbnails which seems to work fine, however I cannot get the pagination to work correctly. I have tried setting the amount of posts shown through the reading settings and the query but still doesn't seem to help! My code is as follows:
<?php get_header(); ?>
<?php query_posts( 'category_name=portfolio' ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_post_thumbnail('portfolio-thumb'); ?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<p>Show project details</a></p>
<?php endwhile; ?>
<div class="alignleft"><?php next_posts_link('← Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries →') ?></div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
Joachim Kudish answers:
change your query_posts to the following:
$page = get_query_var( 'page' );
query_posts( array('category_name' => 'portfolio', 'posts_per_page' => 10, 'paged' => $page));
note: change the 10 to whatever you want per page.
more info on the topic here: http://codex.wordpress.org/Function_Reference/query_posts#Pagination_Parameters
Joachim Kudish comments:
I had a typo in there which I corrected, so in case you were super quick, please check it again :)
Thanks for your prompt and detailed answer Joachim, that seems to work...
Only thing is when you navigate to the next page there is no link for 'newer entries' to go back, also when on the last page the 'older entries' link still shows, even though there is no older entries! Any idea how to change this?
Denzel Chia answers:
remove <?php wp_reset_query(); ?>
from your code.
You are resetting the query with this code, it should not be reset,
WordPress need to keep the query so as to know what to show for next page if there is any post.