Change number of posts on consecutive category pages (Wordpress)

前端 未结 1 2019
执念已碎
执念已碎 2021-01-25 02:14

I am trying change the number of posts that are displayed on a category pages to change on consecutive pages (page 2, 3, etc). So page one displays 7 posts, but pages 2, 3 and 4

相关标签:
1条回答
  • 2021-01-25 03:06

    This is from an answer I recently done on WPSE. I have made some changes to suite your needs. You can go and check out that post here

    STEP 1

    If you have changed the main query for a custom query, change it back to the default loop

    <?php
    
            if ( have_posts() ) :
                // Start the Loop.
                while ( have_posts() ) : the_post();
    
                    ///<---YOUR LOOP--->
    
                endwhile;
    
                    //<---YOUR PAGINATION--->   
    
                else : 
    
                    //NO POSTS FOUND OR SOMETHING   
    
                endif; 
    
        ?>
    

    STEP 2

    Use pre_get_posts to alter the main query to change the posts_per_page parameter on the category pages

    STEP 3

    Now, get the posts_per_page option set from the back end (which I assume is 6) and also set your offset which we are going to use. That will be 1 as you will need 7 posts on page one and 6 on the rest

    $ppg = get_option('posts_per_page');
    $offset = 1;
    

    STEP 4

    On page one, you'll need to add the offset to posts_per_page will add up to 7 to get your seven posts on page one.

    $query->set('posts_per_page', $offset + $ppp);
    

    STEP 5

    You must apply your offset to all subsequent pages, otherwise you will get a repetition of the last post of the page on the next page

    $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
    $query->set('posts_per_page',$ppp);
    $query->set('offset',$offset); 
    

    STEP 6

    Lastly, you need to subtract your offset from found_posts otherwise your pagination on the last page will be wrong and give you a 404 error as the last post will be missing due to the incorrect post count

    function category_offset_pagination( $found_posts, $query ) {
        $offset = 1;
    
        if( !is_admin() && $query->is_category() && $query->is_main_query() ) {
            $found_posts = $found_posts - $offset;
        }
        return $found_posts;
    }
    add_filter( 'found_posts', 'category_offset_pagination', 10, 2 );
    

    ALL TOGETHER

    This is how your complete query will look like that should go into functions.php

    function ppp_and_offset_category_page( $query ) {
      if ($query->is_category() && $query->is_main_query() && !is_admin()) {
        $ppp = get_option('posts_per_page');
        $offset = 1;
        if (!$query->is_paged()) {
          $query->set('posts_per_page',$offset + $ppp);
        } else {
          $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
          $query->set('posts_per_page',$ppp);
          $query->set('offset',$offset);
        }
      }
    }
    add_action('pre_get_posts','ppp_and_offset_category_page');
    
    function category_offset_pagination( $found_posts, $query ) {
        $offset = 1;
    
        if( !is_admin() && $query->is_category() && $query->is_main_query() ) {
            $found_posts = $found_posts - $offset;
        }
        return $found_posts;
    }
    add_filter( 'found_posts', 'category_offset_pagination', 10, 2 );
    
    0 讨论(0)
提交回复
热议问题