douhui5529 2016-01-06 02:50 采纳率: 100%
浏览 26
已采纳

Wordpress,在子页面上显示更多帖子

i want to show more posts on my subpages

My code in the 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 the first page i get a wrong pagination. It shows a link to subpage 4 but a subpage 4 doesn't exit.

I think i must add something like this

....
if ( ! $paged || $paged < 2 ) 
{

// show only 10 posts but calculate the pagination with 18 posts

} 
.....

Is this possible?

  • 写回答

2条回答 默认 最新

  • dongna9185 2016-01-09 05:35
    关注

    Here is a modified version of a post I have done on WPSE a while ago

    FROM WPSE

    STEP 1

    We neet to get the posts_per_page option set from the back end (which should be set to 10) and also set your offset which we are going to use. That will be 14 as you will need 24 posts on page one and 24 on the rest.

    If you don't want to alter the posts_per_page option, you can then just simply set the variable $ppg to 10

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

    STEP 2

    On page one, you'll need to subtract the offset to posts_per_page

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

    STEP 3

    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 = ( ( $query->query_vars['paged']-1 ) * $ppp ) - $offset;
    $query->set( 'posts_per_page', $ppp );
    $query->set( 'offset', $offset ); 
    

    STEP 4

    Lastly, you need to add your offset to found_posts otherwise your pagination will will not show the 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 is how your complete query will look like that should go into 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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算