I have categories driven site. I have different template for each category. I would like to set different number of posts â different for each category. Plus I would like to add proper previous and next links in each category.
for example in this category-1.php
I want 4 posts per page:
<?php query_posts('showposts=4'); ?>
<a href="<?=next_posts()?>"> PREVIOUS </a>
<a href="<?=previous_posts()?>"> NEXT </a>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
...
<?php endwhile; endif; ?>
but in this example next_posts()
and previous_posts()
doesn’t work.
As @StephenHarris pointed out there’s also the
pre_get_posts
filter.Example altered from Codex example.
You can use the
pre_get_posts
hook (conditionals are available):You probably only want to change the ‘main query’, so the first check is important. If you are using WP < 3.3 then you can check instead:
Parse Request Action
Not shure if this will work – it may be that the conditionals are not working at this point…
Posts Limit filter
This would be the other chance, as maybe conditionals are already available there.
Note: Both functions are not tested. Plus: Use one or the other. Both should be placed in your functions.php file.
With query_posts, pagination won’t work correctly, unless you use query_posts() in a page template and you set the ‘paged’ query var appropriately: http://codex.wordpress.org/Template_Tags/query_posts
But it may be easier to use a plugin called Custom Post Limits and leave your category page loops the same. You can set different first page and paged limits for each category. See WordPress ⺠Custom Post Limits « WordPress Plugins
I have flexible way where is possible to set any per_page value for each/any woprdpress site page:
open functions.php of the current wp theme
use smth like next:
*