php - Wordpress, display more posts on a subpages -


i want show more posts on subpages

my code in functions.php

function number_of_posts($query) {     if($query->is_main_query())     {        $paged = $query->get( 'paged' );        if ( ! $paged || $paged < 2 )         {         }         else         {            $query->set('posts_per_page', 24);        }     }     return $query; } add_filter('pre_get_posts', 'number_of_posts'); 

problem: on first page wrong pagination. shows link subpage 4 subpage 4 doesn't exit.

i think must add this

.... if ( ! $paged || $paged < 2 )  {  // show 10 posts calculate pagination 18 posts  }  ..... 

is possible?

here modified version of post have done on wpse while ago

from wpse

step 1

we neet posts_per_page option set end (which should set 10) , set offset going use. 14 need 24 posts on page 1 , 24 on rest.

if don't want alter posts_per_page option, can set variable $ppg 10

$ppg = get_option( 'posts_per_page' ); //$ppg = 10; $offset = 14; 

step 2

on page one, you'll need subtract offset posts_per_page

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

step 3

you must apply offset subsequent pages, otherwise repetition of last post of page on next page

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

step 4

lastly, need add offset found_posts otherwise pagination will not show last page

add_filter( 'found_posts', function ( $found_posts, $query )  {     $offset = 14;      if( $query->is_home() && $query->is_main_query() ) {         $found_posts = $found_posts + $offset;     }     return $found_posts; }, 10, 2 ); 

all together

this how complete query should go functions.php

add_action('pre_get_posts', function ( $query )  {     if ( !is_admin() && $query->is_main_query() ) {         $ppp    = get_option( 'posts_per_page' );         //$ppp  = 10;         $offset = 14;          if ( !$query->is_paged() ) {             $query->set( 'posts_per_page', $ppp - $offset );         } else {             $offset = ( ( $query->query_vars['paged']-1 ) * $ppp ) - $offset;             $query->set( 'posts_per_page', $ppp );             $query->set( 'offset', $offset );         }     } });  add_filter( 'found_posts', function ( $found_posts, $query )  {     $offset = 14;      if( $query->is_main_query() ) {         $found_posts = $found_posts + $offset;     }     return $found_posts; }, 10, 2 ); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -