WordPress Pagination – Paged returns Empty Page

I have a pagination function, that shows the page number correctly, however upon clicking on next page e.g 2 it simple shows a white blank page, no matter what I do, I tried doing all possible options to atleast show me the error, but nothing at all.. I have a pagination function that’s work perfectly when I used that in shortcodes.. but in any page, if I pass page.php?page_id=1&paged=2 anywhere paged=2 in GET, it shows blank page…

the structure of pages is like this:

Read More

index.php has the following code..

 get_header();  
 get_template_part( 'templates/_content' );
 get_footer();

and in _content.php template file, I have following code..

if(is_home() OR is_category()) {

    global $paged, $wp_query;
    if(empty($paged)) $paged = 1;

    $posts_per_page = 2;
    $options = array(
      'post_type' => 'post',
      'posts_per_page' => $posts_per_page,
      'paged'          => $paged,
      'post_status'    => 'publish'
    );

    $wp_query = new WP_Query($options);
    $pages = $wp_query->max_num_pages;

    $custom_pagination = custom_pagination($pages,$posts_per_page);
    if($wp_query->have_posts()) : 
        while($wp_query->have_posts()) : the_post();
            get_template_part( 'templates/blog_archive_template' );     
        endwhile;
    else:
        echo '
            <h2 class="center">Not Found !</h2>
            <p class="center">Sorry, but you are looking for something that isn't here.</p>';
    endif; 

    echo $custom_pagination;

} else {    
  if(have_posts()) : while(have_posts()) : the_post();
  /* rest of html code */
}

Can someone please point out single thing that would help me. Thanks for your time.

regards

Related posts

1 comment

  1. The default posts per pages will be 10. You’ve set it to 2 inside your custom query but by that point WordPress has already determined that there’s no need for a page 2 and is instead showing the 404 template.

    You need to modify the main query using pre_get_posts instead.

    Example:

    /**
     * Modify the main query on the posts index or category 
     * page. Set posts per page to 2.
     *
     * @param object $query
     */
    function wpse_modify_home_category_query( $query ) {
    
        // Only apply to the main loop on the frontend.
        if ( is_admin() || ! $query->is_main_query() {
            return false;
        } 
    
        // Check we're on a posts or category page.
        if ( $query->is_home() || $query->is_category() ) {
            $query->set( 'posts_per_page', 2 );
        }
    }
    add_action( 'pre_get_posts', 'wpse_modify_home_category_query' );
    

    Once you’ve added this to functions.php Remove your custom query from the index.php template and use the regular loop.

    If the only thing that’s changing is the number of posts per page and it should apply globally then you’d be better off changing the value in the admin panel on the Settings -> Reading page.

Comments are closed.