I have one page in my template where I would like to set a custom posts_per_page.
Here is the code I have used:
<?php
global $query_string;
query_posts($query_string . '&posts_per_page=4');
if ( have_posts() ) : while ( have_posts() ) : the_post();
...
Now this code limits only 4 items per page and shows pageinate_links below as I have written. However clicking on any other page will lead to a 404.
If I take out the global and query_posts line then it works fine.
This is the paginate_links function I am using:
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => 'Previous',
'next_text' => 'Next',
) );
Use a
pre_get_posts
action in yourfunctions.php
with conditional tags, and remove the call toquery_posts
:However, you can make it work with pagination by passing
paged
parameter.Example –