WordPress.org

Ready to get started?Download WordPress

Forums

Single Post Homepages in 1.3a (23 posts)

  1. brehaut
    Member
    Posted 9 years ago #

    Hi, I am currently developing a site based around wordpress (1.3a, from the nightlies) that has a single post on the front page. I also want to have 10 posts per page for categories and archive pages.
    Currently, to achieve that I have to set the posts per page to be 10, and just don't use all of the wordpress loop on the home page (checking it with is_home()). This works, except for two problems.
    Firstly, the previous / next links (generated by posts_nav_link()) will skip 9 posts when the go to the second page.
    Secondly, it must be slightly inefficient to get 10 posts out of the DB and then discard 9 of them. I would quite like to fix that.
    Does anyone have any ideas for a solution to this.
    Thanks,
    Andrew Brehaut

  2. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    Try this plugin. You'll need the latest nighly.
    <?php
    /*
    Plugin Name: Custom Posts Per Page
    Version: 1.0
    Plugin URI: http://wordpress.org/#
    Description: Change the number of posts per page displayed for different page types.
    Author: Ryan Boren
    Author URI: http://boren.nu/
    */
    $num_posts_on_home = 1;
    function custom_posts_per_page($query_string) {
    global $num_posts_on_home;
    $query = new WP_Query();
    $query->parse_query($query_string);
    if ($query->is_home) {
    if ($query_string != '') {
    $query_string .= '&';
    }
    $query_string .= "posts_per_page=$num_posts_on_home";
    }
    // Category example.
    // else if ($query->is_category) {
    // if ($query_string != '') {
    // $query_string .= '&';
    // }
    // $query_string .= "posts_per_page=$num_posts_in_category";
    // }
    return $query_string;
    }
    add_filter('query_string', 'custom_posts_per_page');
    ?>

  3. brehaut
    Member
    Posted 9 years ago #

    Cheers, thats just the sort of thing i was looking for.
    i made one slight change to the 'if is_home' section, so that it will replace the existing posts_per_page rather than adding a second (which was getting ignored).
    if you care, here is the code:
    if ($query->is_home) {
    if (preg_match("/posts_per_page=/", $query_string)) {
    $query_string = preg_replace("/posts_per_page=[0-9]*/"
    ,"posts_per_page=$num_posts_on_home"
    ,$query_string);
    } else {
    if ($query_string != '') {
    $query_string .= '&amp;';
    }
    $query_string .= "posts_per_page=$num_posts_on_home";
    }
    }

  4. Chad_Coleman
    Member
    Posted 9 years ago #

    Does that code go into the plugins folder under wp-content?
    Or do you paste it into the index somewhere??

  5. divrom
    Member
    Posted 9 years ago #

    rboren, how would/could I adapt that for posts-by-author?

  6. davidprince
    Member
    Posted 9 years ago #

    Wow.. It took me forever to find this but thanks brehaut and rboren this is exactly what I had been looking for and works like a charm!!
    Cheers!

  7. dtweney
    Member
    Posted 9 years ago #

    This post works great -- thanks, Ryan! However, I've noticed one oddity -- whenever this plugin is activated, the WP admin pages start acting funny. Specifically, pages don't appear completely after taking an action such as saving a new post or modifying a template -- instead, I often get a blank HTML document instead of the WP admin interface. The site itself works fine; it's just the admin interface that is going screwy, and even that works--it's just not re-displaying the interface after taking action.

    Any ideas out there why this is happening, or how to fix it?

  8. slbc
    Member
    Posted 9 years ago #

    This is EXACTLY what I needed! Works perfectly. Thank you so much! :)

  9. ColourDreamer
    Member
    Posted 9 years ago #

    Does this plugin mess with feeds? Check this out: http://forums.feedburner.com/viewtopic.php?p=4301#4301

    My feeds no longer work. I don't know why. But I added this plugin around that time.

  10. ColourDreamer
    Member
    Posted 9 years ago #

    The reverse order makes sense for most cases, but on the site I'm suing it, I'd like the posts to show in default order. Is there a way to do that with this plugin?

  11. Geoffrey Sneddon
    Member
    Posted 8 years ago #

    Is there anyway to have the plugin affect only page 1?

  12. Geoffrey Sneddon
    Member
    Posted 8 years ago #

    Hmm... Changed the if to:
    if ($query->is_home && (!isset($_GET['paged']) || $_GET['paged'] == 1)) {

    But I'm now missing the 2nd and 3rd posts.

  13. spencerp
    Member
    Posted 8 years ago #

    Awesome plugin! Works great! Thanks guys... =)

    spencerp

  14. laxstar2
    Member
    Posted 8 years ago #

    does this work in WP2???

  15. superjux
    Member
    Posted 8 years ago #

    Hi, I was also wondering if this works in WP 2.

    Thanks!

  16. superjux
    Member
    Posted 8 years ago #

    Nevermind...tried it, and it's working.

  17. spencerp
    Member
    Posted 8 years ago #

    Sorry it took awhile to reply Hilary.. =( Was watching some movies tonight..anyway, yeah it's working just fine on my WP Version 2.0. =)Reminds me, I gotta reset the amount on my index page again...maybe lower it a bit lol..

    spencerp

  18. TDAlexander
    Member
    Posted 8 years ago #

    I'm trying to modify this slightly so that I can have a specific category display only a single post per page. To do so I've added:

    if (is_category('9')) {
    $num = $posts_per['photoblog'];
    } else {
    $num = $posts_per['category'];
    }

    under the elsif for the $query->is_category, but it seems to be ignoring it. What am I missing?

    Thanks,

    -Tim

  19. Diplo
    Member
    Posted 8 years ago #

    Hello,

    Do you think it's possible to adapt it so that it displays only the last posts per category on index.php? How would the code look like?

    Thanks,

    Diplo

  20. crayola
    Member
    Posted 8 years ago #

    Thanks so much.

    It's exxxxxactly what I was looking for!

  21. landun
    Member
    Posted 8 years ago #

    If you're going to use the plugin for categories, you need to define $num_posts_in_category and register it as a global variable.

    $num_posts_on_home = 1;
    $num_posts_in_category = 20;
    function custom_posts_per_page($query_string) {
    global $num_posts_on_home;
    global $num_posts_in_category;
    $query = new WP_Query();
    ...

  22. kalico
    Member
    Posted 7 years ago #

    This looks like a great plugin, but it lacks any explicit directions. Can someone please explain exactly HOW to use it? I have uploaded it and activated it....now what?

  23. Bhooshan
    Member
    Posted 7 years ago #

    If the development work on this plugin has been completed, someone should introduce the plugin by documenting the installation procedures and a brief overview/demo of what it actually accomplishes.

Topic Closed

This topic has been closed to new replies.

About this Topic