仅在 WordPress 主页上使用功能

乔什

我试图在主页的第一页上只显示 3 个帖子,然后在随后的页面上显示 6 个帖子。对于所有内部页面,我只想要通常的每页 6 个帖子。我有这个工作,但它正在网站的所有页面上进行,而不仅仅是主页。我试过 is_front_page 但这没有帮助。

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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章