WordPress.org

Ready to get started?Download WordPress

Forums

Display more posts for categories than on index.php (17 posts)

  1. Chad
    Member
    Posted 4 years ago #

    Basically, I need the home page to display only 2 posts at a time, but would like 10-15 on all archive and search pages (these are only displaying titles and meta data)

    At the moment I'm using the following at the start of the loop to limit the number of posts in index.php to 2:

    <?php static $count = 0;
    if ($count == "2") { break; }
    else { ?>

    (this code came from Perishable Press: http://perishablepress.com/press/2007/08/06/super-loop-exclude-specific-categories-and-display-any-number-of-posts/)
    and setting the number of posts on the Reading Settings page to 15.

    This works perfectly with one exception. The next/previous posts links jump by 15, instead of 2.

    I understand why this is happening, and should have seen it coming, I'm just not sure what the workaround is.

    Thanks in advance!

  2. jkovis
    Member
    Posted 4 years ago #

    I'd look into modifying the default query on your index.php via query_posts(). For example, something like:

    // modify the query to only show two posts per page
    query_posts($query_string.'&posts_per_page=2');
    
    // run The Loop as normal once the query has been modified
    if ( have_posts() ) : while ( have_posts() ) : the_post();
  3. Chad
    Member
    Posted 4 years ago #

    Got me partway there! The navigation links now move the correct number of posts, but after the first couple of jumps, it hits a 404 error.

    Not sure if this is because it thinks it's already gone through them all due to the Readings Settings value or not. I tried switching to posts_nav _link() (which I didn't really want to use, just as a test) and had the same problem.

    I'll keep working on it, but if anyone has more ideas, I'd love to hear them!

  4. jkovis
    Member
    Posted 4 years ago #

    hmm...can you post a link to your site?

    maybe try this in the meantime:

    query_posts($query_string."&posts_per_page=2&paged=$paged");
  5. esmi
    Forum Moderator
    Posted 4 years ago #

    Or how about:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'posts_per_page' => 2,
    	'paged' => $paged
    );
    query_posts($args);
    ?>
  6. Chad
    Member
    Posted 4 years ago #

    Unfortunately running into the same problem, but thanks... I can't post a link because this isn't live, I'm testing on a local installation.

    Esmi, your code is grabbing the page number just fine (I echoed it out to test it) but still popping up the error. Do you think this is because if 10 posts are set to display, then page '3' would normally display 21-30, but we're looking for 5-6?

    Maybe I'm coming at this backwards. Instead of having index.php display fewer than the default setting, is there a way to get other pages to exceed the default?

  7. esmi
    Forum Moderator
    Posted 4 years ago #

    Do you think this is because if 10 posts are set to display

    No. query_posts is over-writing that via 'posts_per_page' => 2.

    Are there any other Loops on this page?

  8. Chad
    Member
    Posted 4 years ago #

    No, just the one. This is baffling.

  9. esmi
    Forum Moderator
    Posted 4 years ago #

    is there a way to get other pages to exceed the default

    Yes - by using a similar implementation of query_posts on all of your other templates with 'posts_per_page' set to 10. But, in my opinion, that would be going about it backwards. The most efficient approach would be to change index.php since that's the only one that you really want to customise. The code snippet above should work.

    Are you using any post restriction plugins? Anything that might hide a published post?

    Perhaps it's something else in that template? Can you drop a copy of index.php into the WordPress pastebin and post the pastebin url here? Perhaps someone will spot the issue.

  10. jkovis
    Member
    Posted 4 years ago #

    The code snippet above should work.

    I agree that the code should work, but I spent some time yesterday trying to get it to work on a localhost install and found that pagination does break after a couple pages as LynneAndChad described.

    I just set up a test blog (WP 2.8.6 via svn check out and no plugins activated) at http://test.tontinehouse.com/ using the default theme with a couple of tweaks (so you can see the $wp_query->query_vars array and $wp_query->request that WP runs to generate each page).

    I published nine dummy posts and set the blog to show 3 posts per page (via /wp-admin/options-reading.php) by default and then added the following $wp_query edit in index.php:

    // if generating a page off index.php change
    // the posts_per_page setting to 1
    if( is_home() ) {
        query_posts($query_string."&posts_per_page=1&paged=$paged");
    }

    Pagination works for /page/2/ and /page/3/ but breaks with /page/4/ and beyond.

    What's interesting to note is the LIMIT part of $wp_query->request because posts_per_page seems to revert to the default posts per page value (3 in this case) after page three.

    On http://test.tontinehouse.com/page/2/

    ORDER BY wp_2_posts.post_date DESC LIMIT 1, 1

    On http://test.tontinehouse.com/page/4/

    ORDER BY wp_2_posts.post_date DESC LIMIT 9, 3

    I also followed several good WP sources (nathanrice.net and weblogtoolscollection.com, Codex Custom Queries, and Codex Query Overview ) without luck.

  11. Mike Little
    Member
    Posted 4 years ago #

    Try using this at the top of your loop.

    $wp_query = new WP_Query();
    $wp_query->query($query_string."&posts_per_page=2&paged=$paged");

    Basically it's making sure the global wp_query object is populated correctly. The navigation functions require it.

    Mike

  12. Chad
    Member
    Posted 4 years ago #

    @jkovis Glad (sort of) to know its not just me.

    @MikeLittle I'll give it a shot! Thanks!

    @esmi Thanks for all your help. I always look forward to seeing you jump on a thread. :)

  13. Chad
    Member
    Posted 4 years ago #

    Sadly, no luck with Mike's suggestion... still getting the 404

    Here's the pastebin link, in case anyone else can see what I'm missing:
    http://wordpress.pastebin.ca/1701247

  14. Chad
    Member
    Posted 4 years ago #

    Ha... no one else? ;)

  15. jkovis
    Member
    Posted 4 years ago #

    I added the code that MikeLittle suggested, but I'm also still getting a 404 on /page/4/ and beyond

    fine - http://test.tontinehouse.com/
    fine - http://test.tontinehouse.com/page/2/
    fine - http://test.tontinehouse.com/page/3/
    404 - http://test.tontinehouse.com/page/4/
    404 - http://test.tontinehouse.com/page/5/

  16. beradmin
    Member
    Posted 4 years ago #

    I was trying to make my Archive list show more than the default number of posts and I found a simple answer. You might be able to use the same idea in your homepage but to list less instead of more posts.

    Where: http://codex.wordpress.org/Template_Tags/query_posts

    How to: Place a call to query_posts() in one of your Template files before The Loop begins. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the $query_string global variable in the call to query_posts().

    E.g. My default is 3 posts. So I included query_posts() in my Archive template by changing
    <?php if (have_posts()) : ?>
    to
    <?php query_posts('posts_per_page=10'); if (have_posts()) : ?>

    And it worked.

    So try to keep 10 or 15 posts as your default number and add <?php query_posts('posts_per_page=2'); if (have_posts()) : ?> to your homepage.

  17. belg4mit
    Member
    Posted 4 years ago #

    Experiencing the same issue when adding a loop (with an explicit pages_per_post value that is less than the site setting) to a page template to show recent posts in addition to the content of the home page.

    Works fine if I artificially restrict the post per page settings to match the query, but that means archive and category pages are very short.

    Lovely WP lameness

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.