Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Wordpress custom loop pagination WordPress

  • SOLVED

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(); ?>

<div id="content">


<?php query_posts( 'category_name=portfolio' ); ?>

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


<div class="post" id="post-<?php the_ID(); ?>">

<div class="box">

<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_post_thumbnail('portfolio-thumb'); ?>
</a>

</div>

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

<div class="entry">
<p>Show project details</a></p>
</div>

</div>

<?php endwhile; ?>


<div class="navigation">
<div class="alignleft"><?php next_posts_link('&larr; Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries &rarr;') ?></div>
</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(); ?>

</div>

<?php get_footer(); ?>

Answers (3)

2011-01-28

Joachim Kudish answers:

change your query_posts to the following:

<?php
$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 :)


Bento82 comments:

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?

Thanks again!

Ben

2011-01-28

S├ębastien | French WordpressDesigner answers:

<?php query_posts( 'category_name=portfolio&paged='.$paged ); ?>


S├ębastien | French WordpressDesigner comments:

is it ok bento ?

2011-01-28

Denzel Chia answers:

Hi,

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.

Thanks
Denzel