• Hello people,

    Recently, I was trying to hide the first 5 posts from the homepage, so I came up with the following code for theloop.php file:

    <?php
    	global $post, $query_string, $SMTheme;
    	query_posts($query_string);
    	$i=1;
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $posts_per_page = 5;
    $offset = $posts_per_page * ($paged - 1) + 5;
    $args = array(
      'posts_per_page' => $posts_per_page,
      'paged' => $paged,
      'offset' => $offset,
    );
    	if (is_home()||is_front_page())
    	query_posts($args);
    	if (have_posts()) :  
    
    	if (!isset($_GET['ajaxpage'])) {?>
     <div class='articles'>
    	<?php }
    	while (have_posts()) : the_post();
    	?>

    The code works great, except for a strange issue: the pagination generates an extra (last) page with nothing in it, completely empty.

    Is it posible to do a workaround of the code to prevent for this last empty page being generated?

    Thanks in advance!

    Best Regards.

    PS: single.php file just does call to theloop.php, so editing single.php won’t help at all. This is the code of the pagination, if needed:

    <?php if (is_single()) { ?>
    		<div class="navigation">
    				<div class="alignleft"> <?php previous_post_link('%link', '← %title', true); ?></div>
    				<div class="alignright"><?php next_post_link('%link', '%title →', true); ?></div>
    		</div>
    		<?php  } ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Marius L. J.

    (@clorith)

    Hi,

    I suspect this is caused by your use of query_posts, in this case you should be using the pre_get_posts hook.

    Also, I’d recommend having a look at the WP_Query class for future reference, as it is what people often mistakenly forget about when using query_posts.

    Thread Starter BGH_

    (@bgh_)

    Thank you for your answer, Marius!

    Unfortunately, I don’t get how should I use pre_get_posts hook. From what I read here, I suppose that it should be something like this:

    function exclude_first_five_posts_home( $query ) {
        if ($query->is_home() && $query->is_main_query()) {
            $query->set( 'offset', '5' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_first_five_posts_home' );

    I added that code to functions.php, and deleted the code from theloop.php, so it stays like this:

    <?php
    	global $post, $query_string, $SMTheme;
    	query_posts($query_string);
    	$i=1;
    	if (have_posts()) :  
    
    	if (!isset($_GET['ajaxpage'])) {?>
     <div class='articles'>
    	<?php }
    	while (have_posts()) : the_post();
    	?>

    But it didn’t work, as it shows the first five 5 post which is what I want to avoid without breaking the pagination.

    What am I doing wrong?

    Regards.

    Moderator Marius L. J.

    (@clorith)

    Awesome work with the hook, that’s exactly how it should be done.

    As for the second part, you want to use The Loop as normal, no custom query is needed at this point as the hook took care of the magic for you behind the scenes.

    Thread Starter BGH_

    (@bgh_)

    Ok but assuming I should remove query_posts($query_string);, then with that function, indeed hides the first five posts on the homepage, but it repeats the same posts all over the pages. This is how it shows:

    Page 1: 6th post, 7th post, 8th post, 9th post, 10th post.
    Page 2: 6th post, 7th post, 8th post, 9th post, 10th post.
    Page 3: 6th post, 7th post, 8th post, 9th post, 10th post.

    But I want to show it like this:

    Page 1: 6th post, 7th post, 8th post, 9th post, 10th post.
    Page 2: 11th post, 12th post, 13th post, 14th post, 15th post.
    Page 3: 16th post, 17th post, 18th post, 19th post, 20th post.

    I think I need to use $paged, but I don’t know how it should be implemented in this hook

    Moderator Marius L. J.

    (@clorith)

    Nah, no need for pagination here, but you need to use a new WP_Query instead of query_posts in this case

    Thread Starter BGH_

    (@bgh_)

    Thanks for your help in this issue, but to be honest I’m a bit confused because, according to the example given on the wordpress codex, I edited my theloop.php file like this:

    <?php
    	global $post, $query_string, $the_query, $args, $SMTheme;
    	$the_query = new WP_Query( $args );
    	$i=1;
    	if ($the_query->have_posts()) :  
    
    	if (!isset($_GET['ajaxpage'])) {?>
     <div class='articles'>
    	<?php }
    	while ($the_query->have_posts()) : $the_query->the_post();
    	?>

    And now all the posts have just dissapeared! There’s only the pagination left. Even when I hit a post from a recent post widget I hit, the whole content is ripped off and there’s only the comments left. Is amusing because I’m understanding this the wrong way, it seems xD.

    Moderator Marius L. J.

    (@clorith)

    Could you pastebin the whole code you are using, and I’ll have a look at it, it’s a bit hard when it’s fractured in little bits like here ??

    Thread Starter BGH_

    (@bgh_)

    Done. I divided it in 3 commented lines (//): 1st one is the whole theloop.php file, 2nd one is the function I tried using the pre_get_posts hook, and the 3rd one is another function which also uses the pre_get_posts hook, though it is unrelated to this issue, but posted it in case you need it.

    Here you go and thanks again :).

    https://pastebin.com/VQn4ygVk

    Moderator Marius L. J.

    (@clorith)

    All right, try something along these lines: https://gist.github.com/Clorith/92ed18ec3f447558047b (I didn’t include all your stuff, just showing you how it should be laid out)

    Thread Starter BGH_

    (@bgh_)

    Tried it, but it didn’t work, as unfortunately, that part in my loop file is a bit hard coded, and I can’t figure it out how to implement it. Tried a myriad of ways but it always ended on: 1) giving the classic copyright error from SMThemes (which I didn’t alter) and not showing any posts at all not even the admin bar of the top, or 2) always printing the same posts from page 1 to the last page.

    This is basically the modified file: https://pastebin.com/LVVNi3V7

    Thanks for all your help, Marius!

    Regards.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Empty page last page generated by pagination’ is closed to new replies.