Forums

[resolved] Displaying all Posts on Category Pages (8 posts)

  1. ChrisStoneman
    Member
    Posted 8 months ago #

    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?

    Thanks

  2. Jason
    Member
    Posted 8 months ago #

    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.

  3. ChrisStoneman
    Member
    Posted 8 months ago #

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

  4. Jason
    Member
    Posted 8 months ago #

    How about like this?

    <?php
    $cat_id = get_cat_ID( single_cat_title(null, false) );
    query_posts( "cat=$cat_id&posts_per_page=10" );
    ?>
  5. ChrisStoneman
    Member
    Posted 8 months ago #

    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!

  6. Jason
    Member
    Posted 8 months ago #

    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.

  7. Jason
    Member
    Posted 8 months ago #

    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>
    </div>
  8. ChrisStoneman
    Member
    Posted 8 months ago #

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

Reply

You must log in to post.

About this Topic