Support » Themes and Templates » Displaying all Posts on Category Pages

  • Resolved ChrisStoneman


    My menu contains my main category headings, and displays a number of posts from that category, as here.

    My theme, The Erudite, as an option to choose how many posts are displayed on the homepage. This selection also controls how many posts are displayed on the category page. How do I override this so I can display more posts on my category pages than on my home page?


Viewing 7 replies - 1 through 7 (of 7 total)
  • In category.php, before the loop starts here:

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

    … Maybe try using query posts to change the number of posts shown for that specific template. So, just before the loop starts you’d put this:

    <?php query_posts( 'posts_per_page=10' ); ?>

    In this case 10 posts per page would show.

    This works, but pulls the last 10 post’s from all categories. How do I pull the last 10 posts from only that category?

    How about like this?

    $cat_id = get_cat_ID( single_cat_title(null, false) );
    query_posts( "cat=$cat_id&posts_per_page=10" );

    Again this works, but now the ‘older posts’ link at the bottom of the page doesn’t work. If I ask the page to display 10 posts from that category, the ‘older posts’ link should click through to the previous 10, but now it clicks through to the same 10??
    Thanks again!

    haha this is getting more complicated than I thought at first glance originally.

    Try it like this:

    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $cat_id = get_cat_ID( single_cat_title(null, false) );
    query_posts( "cat=$cat_id&paged=$paged&posts_per_page=10" );

    … This is kind of a hack though, and this doesn’t always work outside of a page template.

    Oh, and by the way I just remembered in your original question you said you wanted to show all posts from the category.

    You could do this to accomplish that without any pagination:

    $cat_id = get_cat_ID( single_cat_title(null, false) );
    query_posts( "cat=$cat_id&posts_per_page=-1" );

    Then, also you could remove this from your category.php file because no pagination would be needed.

    <div id="nav-below" class="navigation">
    	<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'erudite' ) ) ?></div>
    	<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'erudite' ) ) ?></div>

    haha, yes it was more work than you thought, but both of these solutions work perfectly. Thanks so much for your help, much appreciated

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Displaying all Posts on Category Pages’ is closed to new replies.