Hi I have page displaying 20 featured images and info of a specific category, I wish to able to have pagination so I can have multiple pages for the category.

How do I do this? I cant seem to get it work.

Any help would be appreciated


<?php get_header(); ?>
<div class="modus-grid">
     <?php query_posts(array('category__in' => array(5), 'posts_per_page' => 20)); ?>
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                    <?php $currentid = get_the_id(); ?>

                    <div class="grid-box grid-block mod-box width33">

                        <div class="view view-first">                                
                                <?php if (get_the_post_thumbnail($currentid, array(167, 167)) != "") { ?>

                                   <a href="<?php the_permalink(); ?>" /><?php echo get_the_post_thumbnail($currentid, array(300,600)); ?></a>
                                <?php } else { ?> 
                                    <a href="<?php the_permalink(); ?>" /><img src='<?php bloginfo('template_url') ?>/images/default.png' alt='Default'/></a>
                                <?php } ?>

                            <div class="mask">  
                                <h2><a href="<?php the_permalink(); ?>"><?php echo get_the_title($currentid); ?></a></h2>
                                <p><?php echo get_the_excerpt( $post->parent ); ?></p>
                                <a href="<?php the_permalink(); ?>" class="info">Read More...</a>


            <div class="clearfix">
<?php get_footer(); ?>

The problem exists because you are using query_posts(), instead of properly filtering the main query via pre_get_posts.

Remove your query_posts() call from the template file, then add the following to functions.php:

function wpse74325_pre_get_posts( $query ) {
    if ( $query->is_main_query() && is_category( 5 ) ) {
        $query->set( 'posts_per_page', '20' );
add_action( 'pre_get_posts', 'wpse74325_pre_get_posts' );

This function will properly modify the main query object, before retrieving posts, allowing WordPress to assign pagination properly.

| improve this answer | |

You are not passing any pagination parameters. Like this, from the Codex...

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query( 'paged=' . $paged );

Notice that code uses WP_Query not query_posts. Don't use query_posts. It is pretty much never the best choice for creating a loop.

The top of your Loop should look like...

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$my_query = new WP_Query(array('category__in' => array(5), 'posts_per_page' => 20,'paged'=>$paged)); ?>
    <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) :
         the_post(); ?>

And then the rest of your loop.

You probably also want next_posts_link and previous_posts_link.

| improve this answer | |
  • Hi, thanks a lot for help, when I tried the code it created a never ending page I obviously did it wrong. – MrThunder Nov 29 '12 at 19:12
  • Here is the code < i160.photobucket.com/albums/t195/rt840/tedwdws.jpg > – MrThunder Nov 29 '12 at 19:24
  • There is no need to use a custom query when the main query is the one intended to be modified. Let the template system do its job. :) – Chip Bennett Oct 30 '13 at 20:55
  • @ChipBennett : It isn't clear that this is a category archive page, and given that the category is hard-coded-- array(5)-- I am guessing that it is not, which conclusion is supported by the first sentence of the question. – s_ha_dum Oct 30 '13 at 21:02
  • Ah, well if it's the case that it's a custom page template, the solution is covered here. – Chip Bennett Oct 30 '13 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.