WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd be really grateful if anyone has any ideas about what might be wrong with this code or, alternatively, could suggest an entirely different way of achieving the same aims.

I have a site (staged, not public) with a custom post type of 'resources'. Each resource is categorised. I'd like to generate a paginated list – 10 per page – of posts collected under the appropriate category titles.

For example:

Category name 1
– link to post
– link to post

Category name 2
– link to post
– link to post
– link to post
– link to post

Category name 3
– link to post

...and so on.

This is the code I'm using now to generate my category titled list of posts:

<?php $cats = get_categories(); 
foreach ($cats as $cat) { 
$cat_id = $cat->term_id; echo "<h2>".$cat->name."</h2>"; 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$args = array( 'post_type' => 'resources', 'posts_per_page' => 10, 'cat' => $cat_id, paged => $paged ); $wp_query = new WP_Query($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
                    <li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li>

                <?php endwhile; endif; ?>
            <?php } ?>

This works to a point but, unfortunately, does not paginate. I'm using

<?php previous_posts_link('Older resources'); ?>
<?php next_posts_link('Newer resources'); ?>

to generate navigation. I'd also like to only list category titles that have associated 'resource' posts.

I'm sure it's the 'foreach' used to output each category title that's breaking the pagination here, but I'm stumped as to how to fix that or write an alternative.

share|improve this question
The answer offered at here is a more effective version than my code above, since it deals with empty categories more effectively, but still does not paginate. – zeropaul Mar 6 '13 at 20:06
Are you checking if the pagination links should be placed by using: if ( $wp_query->max_num_pages > 1 ) { ... place links ... } – Vivienne Mar 6 '13 at 23:00
Vivienne – thanks, I wasn't, but just tried and it makes no difference here. The problem is that it's not even restricting the posts_per_page to the number specified (eg. if there's 15, categorised 'resource' post types, all 15 are displayed even though the number is set to 10). – zeropaul Mar 7 '13 at 9:46

Aren't you supposed to have single quotes around paged in $args = array( 'post_type' => 'resources', 'posts_per_page' => 10, 'cat' => $cat_id, paged => $paged );

like so:

$args = array( 'post_type' => 'resources', 'posts_per_page' => 10, 'cat' => $cat_id, 'paged' => $paged );

share|improve this answer
I think you're correct about that and, since posting, I'd spotted and corrected it, but it makes no difference either way. Thanks for your input, though! – zeropaul Mar 6 '13 at 21:24

Your Answer


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.