最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Wordpress Sticky Post Count "Fix" Breaking Pagecount by 72 pages!

programmeradmin0浏览0评论

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);
            }
        }
    }
});
发布评论

评论列表(0)

  1. 暂无评论