外贸营销

WordPress posts_per_page如何将第一页的文章数设置成与其他页的数量不同
2019.09.06 外贸营销

最近有个客户提到这样一个需要,就是首页先显示20篇文章,再每页加载10篇(点击加载更多),这样就与一般的分页不一样了,既要保证文章不重复,也要保证分页正确。请看下面代码:

function MBThemes_offset_firstpage( $query ) { if ( $query->is_main_query() && !is_admin() && $query->get('post_type') != 'project') { $offset = 10; $query->set( 'post_type', 'post' ); $query->set( 'post_status', 'publish' ); $query->set( 'ignore_sticky_posts', '-1' ); $ppp = get_option('posts_per_page');  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','MBThemes_offset_firstpage');function MBThemes_offset_firstpage_pagination( $found_posts, $query ) { $offset = 10;if( $query->is_main_query() && !is_admin() && $query->get('post_type') != 'project') { $found_posts = $found_posts - $offset; } return $found_posts;}add_filter( 'found_posts', 'MBThemes_offset_firstpage_pagination', 10, 2 );
上一篇下一篇
标签

有问
必答