Php 仅使用WordPress主页上的功能

Php 仅使用WordPress主页上的功能,php,wordpress,Php,Wordpress,我试图在首页的第一页只显示3篇文章,然后在接下来的页面显示6篇文章。对于所有的内部页面,我只希望每页通常有6篇文章。我有这个工作,但它是做的网站,而不仅仅是主页的所有页面的影响。我试过首页,但没用 add_action( 'pre_get_posts', 'sk_query_offset', 3 ); function sk_query_offset( &$query ) { // Before anything else, make sure this is the righ

我试图在首页的第一页只显示3篇文章,然后在接下来的页面显示6篇文章。对于所有的内部页面,我只希望每页通常有6篇文章。我有这个工作,但它是做的网站,而不仅仅是主页的所有页面的影响。我试过首页,但没用

add_action( 'pre_get_posts', 'sk_query_offset', 3 );
function sk_query_offset( &$query ) {

    // Before anything else, make sure this is the right query...
    if ( $query->is_home() && is_main_query() ) {
        return;
    }

    // First, define your desired offset...
    $offset = -3;

    // Next, determine how many posts per page you want (we'll use WordPress's settings)
    $ppp = get_option( 'posts_per_page' );

    // Next, detect and handle pagination...
    if ( $query->is_paged ) {

        // Manually determine page query offset (offset + current page (minus one) x posts per page)
        $page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );

        // Apply adjust page offset
        $query->set( 'offset', $page_offset );

    }
    else {

        // This is the first page. Set a different number for posts per page
        $query->set( 'posts_per_page', $offset + $ppp );

    }
}

add_filter( 'found_posts', 'sk_adjust_offset_pagination', 1, 2 );
function sk_adjust_offset_pagination( $found_posts, $query ) {

    // Define our offset again...
    $offset = -6;

    // Ensure we're modifying the right query object...
    if ( $query->is_home() && is_main_query() ) {
        // Reduce WordPress's found_posts count by the offset...
        return $found_posts - $offset;
    }
    return $found_posts;
}
以下是主页:www.ninesixty.co.nz/alisonhehir
这是一个内部页面:www.ninesixty.co.nz/alisonhehir/studio

这将在您的主页上显示3篇文章,然后在您的站点上为其余分页页面设置每页文章。我测试了它的其他组合,它似乎仍然工作正常。该函数对于分页非常重要,以确保在计算要显示的总页数时,如果有剩余页数,它会添加一个额外的页。仅当您的主页设置为显示最新帖子时,此选项才起作用。如果你想让它与一个静态页面一起工作,那么这篇文章中涉及的内容就要多得多

add_action( 'pre_get_posts', 'sk_query_offset', 3 );

function sk_query_offset( &$query ) {

    $offset = 3;
    $ppp = get_option( 'posts_per_page' );

    if ( $query->is_home() && $query->is_main_query() && ! $query->is_paged ) {
        $query->set( 'posts_per_page', $offset );
        return;
    }

    if ( $query->is_home() && $query->is_main_query() && $query->is_paged ) {

        if( $query->query_vars['paged'] == 2 ) {
            $page_offset = $offset;
        }
        else {
            $page_offset = $offset + ( ( $query->query_vars['paged'] - 2 ) * $ppp );
        }

        $query->set( 'offset', $page_offset );
    }
}

add_filter( 'found_posts', 'sk_adjust_offset_pagination', 1, 2 );

function sk_adjust_offset_pagination( $found_posts, $query ) {
    $ppp = get_option( 'posts_per_page' );

    // We set this value in sk_query_offset
    $offset_ppp = $query->query_vars['posts_per_page'];

    if ( $query->is_home() && $query->is_main_query() && ! $query->is_paged ) {
        // This is important, we need to always round up to the highest integer for calculating the max number of pages.
        $max_pages = ceil( $found_posts / $ppp );

        return ( $offset_ppp * $max_pages );
    }

    return $found_posts;
}

$query->is_home此条件也指首页。您可以消除或写入条件以显示我上面提到的帖子,我已经尝试了is_front_页面,但它仍然将该功能应用于所有页面。感谢您花时间回复thoughis_首页将无法工作,因为在设置WP_查询之前,pre_get_帖子会运行。不过,is_home可以工作,但最好使用查询变量。您能解释一下或将我链接到相关参考吗?对不起,我还在想办法解决这个WordPress的问题。谢谢你的帮助,但不幸的是,这根本不起作用。这在主页上显示了6篇文章,所以只是显示了defaut功能。你的主页设置如何?我有我的设置来显示博客文章,它只显示3篇文章,你可以设置它来显示一个静态页面,这样它的工作方式就不同了。是的,它是一个静态页面。这样做不可能吗?这需要一点工作才能完成,WordPress已将此解决方案链接到pre_get_posts codex上,以针对您的情况。我现在通过对上面代码的一点修改来实现这一点。唯一的问题是,现在在后端/仪表板的第一页上也只显示了3篇文章。有没有关于如何确保它只在网站前端工作的IDE?