brehaut
Member
Posted 4 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
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');
?>
brehaut
Member
Posted 4 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 .= '&';
}
$query_string .= "posts_per_page=$num_posts_on_home";
}
}
Does that code go into the plugins folder under wp-content?
Or do you paste it into the index somewhere??
rboren, how would/could I adapt that for posts-by-author?
davidprince
Member
Posted 4 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!
dtweney
Member
Posted 3 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?
This is EXACTLY what I needed! Works perfectly. Thank you so much! :)
ColourDreamer
Member
Posted 3 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.
ColourDreamer
Member
Posted 3 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?
Is there anyway to have the plugin affect only page 1?
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.
Awesome plugin! Works great! Thanks guys... =)
spencerp
laxstar2
Member
Posted 2 years ago #
Hi, I was also wondering if this works in WP 2.
Thanks!
Nevermind...tried it, and it's working.
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
TDAlexander
Member
Posted 2 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
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
crayola
Member
Posted 2 years ago #
Thanks so much.
It's exxxxxactly what I was looking for!
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();
...
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?
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.