admin管理员组

文章数量:1323355

I have a permanent sticky post on my front page and have implemented the code (pasted below) into my functions.php file to stop it adding an extra post to my post count on the front page (it's a grid so i can't have one post dangling off the bottom).

However.... when the code is implemented it's adding a whopping 72 extra pages to my page count. From 799 (which is what it should be) to 871. Any page past 799 obviously comes up at 404 because it doesn't exist.

Can someone please help me figure out what's causing it? Cheers

add_action( 'pre_get_posts', function( $query )
{
    if ( $query->is_home && $query->is_main_query() )
    {
        $posts_per_page = get_option( 'posts_per_page' );
        $sticky_posts   = get_option( 'sticky_posts' );

        // if we have any sticky posts and we are at the first page
        if ( is_array($sticky_posts) && !$query->is_paged() )
        {
            $sticky_count = count($sticky_posts);

            if ( $sticky_count < $posts_per_page )
            {
                $query->set('posts_per_page', $posts_per_page - $sticky_count);
            }
            else
            {
                $query->set('posts_per_page', 1);
            }
        }
    }
});

本文标签: functionsWordpress Sticky Post Count quotFixquot Breaking Pagecount by 72 pages