• Chad

    (@lynneandchad)


    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!

Viewing 15 replies - 1 through 15 (of 16 total)
  • 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();
    Thread Starter Chad

    (@lynneandchad)

    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!

    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");

    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);
    ?>
    Thread Starter Chad

    (@lynneandchad)

    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?

    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?

    Thread Starter Chad

    (@lynneandchad)

    No, just the one. This is baffling.

    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.

    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.

    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

    Thread Starter Chad

    (@lynneandchad)

    @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. πŸ™‚

    Thread Starter Chad

    (@lynneandchad)

    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

    Thread Starter Chad

    (@lynneandchad)

    Ha… no one else? πŸ˜‰

    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/

    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.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Display more posts for categories than on index.php’ is closed to new replies.