Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In the Reading Settings, there is place to set the number of posts shown that affects the number of posts shown in all contexts. I'd like to show instead a certain number of posts on the homepage, and a different number of posts on pages like archive, search results, etc.

reading-settings-blog-pages-show-at-most-per-page

I realize I could do this by editing the theme files and changing the query parameters there, but I'd prefer to have easier access to a simple settings page. A long time ago, I came across a plugin that did this, but I can't locate it now.

Does anyone know of a plugin to do this, or even a function I could put in functions.php to accomplish the same thing?

share|improve this question
    
You were probably thinking of @chip-bennett's 'Different Posts Per Page' plugin: chipbennett.net/plugins/cbnet-different-posts-per-page which does what you want in a graphical way. –  Tom Auger Oct 28 '11 at 18:09
add comment

4 Answers 4

up vote 16 down vote accepted

I believe the best way to do this in a plugin is to run the following sample function when the pre_get_posts action hook is encountered. The $wp_query object is available, meaning your conditional tags are available, but before WordPress gets the posts, which means you are changing query vars prior to the first query being run, rather than adding a second query like when query_posts() is used in a theme file.

function custom_posts_per_page($query) {
    if (is_home()) {
        $query->set('posts_per_page', 8);
    }
    if (is_search()) {
        $query->set('posts_per_page', -1);
    }
    if (is_archive()) {
        $query->set('posts_per_page', 25);
    } //endif
} //function

//this adds the function above to the 'pre_get_posts' action     
add_action('pre_get_posts', 'custom_posts_per_page');
share|improve this answer
    
Just tested and it works like a charm. Thanks! –  Doug Aug 12 '10 at 18:22
    
I use this function on my own site. It was easy to cut and paste it here for you. –  kevtrout Aug 12 '10 at 19:11
add comment

you could do a custom loop using query_posts, and specify the number of posts by is_home, is_archive, etc.

just a simple if statement along with query_posts

share|improve this answer
    
Yes, I mentioned that in my question. I'm looking for a way to do it so I don't have to edit multiple theme files to change the settings. Thanks. –  Doug Aug 12 '10 at 18:21
1  
It's rather bad idea to do it this way. WordPress already runs query. If you put query_posts in your theme, you will ignore original query and make another, redundant one. –  Krzysiek Dróżdż Dec 11 '13 at 20:45
add comment

To adding to this question. Does somebody know how to determine post_per_page for this function function custom_posts_per_page($query) if add the new page to index.php like this:

<?php
if ($_GET['new'] == 1) 
{
    include ( TEMPLATEPATH . '/newpage.php' );
    exit;
}
?>
share|improve this answer
add comment

I realize I could do this by editing the theme files and changing the query parameters there, but I'd prefer to have easier access to a simple settings page.

In the interest of completeness, I found that query_posts combined with $query_string concatenation works well.

I placed this code in index.php (my theme doesn't have category.php) ...

<?php 
if (!is_front_page()) { 
    query_posts($query_string . "&posts_per_page=20"); 
}
?>

Of course what we're doing here is modifying the internal query string of the loop, overriding the default number of posts per page.

It works a treat for providing a smaller list of posts on the homepage where I am showing full posts, and a much larger list of posts everywhere else (categories, by date, etc) where I am only showing post summaries.

However, please do note that unlike the accepted answer, this will run the query twice, so it is not as nice a solution.

share|improve this answer
    
Jeff, I was looking for a centralized place to control the number of posts returned in different contexts. Your answer only does that if you use index.php to handle ALL contexts, which most themes don't do. In addition to increasing the calls to the database (which affects performance), your technique can also change the response to conditional tags (e.g., is_home() or is_front_page) in unexpected ways. Finally, if you're wanting to add secondary loops to a page, you should NOT use query_posts, but use "new WP_Query" instead (or get_posts). –  Doug Jul 7 '11 at 19:26
    
@doug this isn't a secondary loop, it's overriding the main loop -- and yes, this particular theme uses index.php to render everything, it's fairly minimalistic. I felt this made more sense here as an answer, but if you'd like I can ask my own question and answer it if this answer offends you in some way. –  Jeff Atwood Jul 7 '11 at 21:51
    
Actually I misread your second-to-last paragraph and thought the "larger" and "smaller" lists of posts were on the same page, so my last sentence above was irrelevant. And yes, it's perfectly legitimate to put all your code in index.php if you want. And if that's the case, then your method is a quick and easy way to do this, so I'll remove my downvote after the 20 hour lock is over. Even so, sending a new query rather than modifying the original query as in kevtrout's answer is less desirable for the reasons already mentioned (by you, me and the WP Codex). –  Doug Jul 8 '11 at 16:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.