Support » Fixing WordPress » wp_query, offset and pagination give an extra (last) empty page

  • Resolved boryanka

    (@boryanka)


    I need to exclude the latest post from the loop.
    I have done this with ‘offset’, but then the pagination didn’t work.
    After researching and googling I have found some snippets of code that did the trick for pagination! Only problem is that I get an extra empty page with no posts on it.

    I have currently only 5 posts (6 with the latest one that I want excluded).
    If I set number of posts per page in settings to 3, everything is ok.
    If I set it to 5, I should get only one page, but the link to second page still appears and on that second page, there is nothing

    Any idea how to work around that?

    This code is in my functions.php:

    function my_post_limit($limit) {
    	global $paged, $myOffset;
    	if (empty($paged)) {
    		$paged = 1;
    	}
    	$postperpage = intval(get_option('posts_per_page'));
    	$pgstrt = ((intval($paged) -1) * $postperpage) + $myOffset . ', ';
    	$limit = 'LIMIT '.$pgstrt.$postperpage;
    	return $limit;
    }

    This code is in my index.php:
    [Code moderated as per the Forum Rules. Please use the pastebin]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter boryanka

    (@boryanka)

    This code is in my index.php:
    http://pastebin.com/DiwFRc3C

    EDGE22

    (@midnightdonkey)

    Hey, was just battling this same issue and came across the solution.

    Add this under your wp_query->query():

    $wp_query->max_num_pages = ceil(($wp_query->found_posts – $featured_posts) / $post_amount);

    $featured_posts should be your offset number, while $post_amount is how many posts you want to show on the page.

    Hope this helps,
    Tom

    Thread Starter boryanka

    (@boryanka)

    Actually I was able to solve this on my own but it was kind of a compromise.
    I took the ID of the featured post, and then in the loop I added the following line:
    if( $post->ID == $exclude_id ) continue;

    No custom functions, no post limit, just one line. The only thing is in the first page, I now have 1 post less than the setting in wp-admin, but this is so tolerable. Pagination works excellent.

    I will look into your code, thanks much for replying.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_query, offset and pagination give an extra (last) empty page’ is closed to new replies.