WordPress.org

Ready to get started?Download WordPress

Forums

Issue with post offset and related posts widget (1 post)

  1. Doodlebee
    Member
    Posted 1 year ago #

    Hey all. I'm working on a site where the first page of the archive shows only 5 posts (while any paginated pages show 10, which is what is in the settings), an the very first post on the first page is styled differently. it's working great, however, I noticed in the sidebar, the "Related Posts" widget is also following the settings for the page content - like the query is interfering with the stuff in the sidebar, too.

    Usually, an added "wp_reset_query();" is all it takes, but it's not working this time.

    This is my query that does the offset and pagination (so 5 posts show up on the first archive page, but it returns to normal on the rest):

    add_action('pre_get_posts', '_set_offset_on_front_page');
    function _set_offset_on_front_page(&$query) {
           if (is_archive() && is_paged()) {
                $posts_per_page = isset($query->query_vars['posts_per_page']) ? $query->query_vars['posts_per_page'] : get_option('posts_per_page');
                // If you want to use 'offset', set it to something that passes empty() - 0 will not work, but adding 0.1 does (it gets normalized via absint())
                // I use + 1, so it ignores the first post that is already on the front page.  The -2 is to make the query recognize what paginated page you're on - skipping page 1
                // the final +5 at the end is offset by how many you're showing on the first page
                $query->query_vars['offset'] = (($query->query_vars['paged'] - 2) * $posts_per_page) + 5;
        }
    }

    The archive.php simply has the following:

    if(!is_paged()) new WP_Query(array('posts_per_page' => '5', 'cat' => $cat));

    and ends with the endif; wp_reset_query();

    But as I said, it's still affecting the sidebar widgets. Anyone have any clue as to why or how I could fix it?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.