Skip to content
Advertisement

WordPress custom pagination links to a blank page

this is my archive of posts: https://polnapol-tarnow.pl/aktualnosci/page/4/

There are only 4 pages but link to another (blank) page is showing up anyway (“Następna strona”). Is there a way to adjust conditions to stop rendering another “Next page” link if there is no another page with posts? I would be very grateful for any help.

Faulty code:

<?php if (!is_paged()) : ?>
 <a href="<?php echo get_pagenum_link($paged+2); ?>" class="page-number-last"><span><?php _e('Next page'); ?> &rsaquo;&rsaquo;</span></a>
<?php else : ?>
 <a href="<?php echo get_pagenum_link($paged+1); ?>" class="page-number-last"><span><?php _e('Next page'); ?> &rsaquo;&rsaquo;</span></a>
<?php endif;  ?>

Full template:

<?php $b_subitlte = get_field('b_subitlte');
$b_title = get_field('b_title');
$b_desc = get_field('b_desc'); ?>

<section class="blog-home padding czarny">
    <?php if ( function_exists('yoast_breadcrumb') ) { ?>
        <div class="breadcrumbs">
            <div class="container">
                <?php yoast_breadcrumb('<p id="breadcrumbs">','</p>'); ?>
            </div>
        </div>  
    <?php   } ?>
    <div class="col-sm-offset-2 col-sm-8 text-center">
                    <h4 class="upper-title">aktualności</h4>
                    <h2 class="title">aktywni <br>
nie tylko&nbsp;w&nbsp;kuchni</h2>
                    <p>nieustannie badamy otaczający świat aby tworzyć nie&nbsp;tylko&nbsp;lepsze potrawy, ale&nbsp;i&nbsp;klimat naszego włoskiego lokalu. Inspirujemy się i&nbsp;piszemy o&nbsp;tym!</p>
                    </div>
            <div class="container">
                <div class="row">
                    <div class="col-sm-offset-2 col-sm-8 text-center">
                    <h4 class="upper-title"><?php echo $b_subitlte; ?></h4>
                    <h2 class="title black"><?php echo $b_title; ?></h2>
                    <p><?php echo $b_desc; ?></p>
                    </div>
                </div>
                <?php
                
$args = array(  
    'posts_per_page' => 5,

    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1 
    

 );


$news = new WP_Query( $args );

if ( $news->have_posts() ) : ?>
                <div class="blog-wrapper row">
                
                <?php while ( $news->have_posts() ) : $news->the_post(); ?>
                <?php $blog_short = get_field('blog_short'); ?>


                    <div class="col-md-4 col-xs-6">
                    <?php if ( has_post_thumbnail() ) : ?>
    
    <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
                        <div style="background-image: url(<?php echo $url; ?>)" class="thumbnail-cover">
                            <?php the_post_thumbnail($post->ID);?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="custom-hover">
                                <span class="main-btn">zobacz post</span>
                            </a>
                        </div>
<?php endif; ?>
                        <p class="meta"><?php the_time('Y-m-d'); ?></p>
                        <h2 class="title">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </h2>
                        <p><?php echo $blog_short; ?></p>
                    </div>

                <?php endwhile; ?>

    </div>
<?php echo previous_posts_link(); ?>

<?php if (!is_paged()) : ?>
 <a href="<?php echo get_pagenum_link($paged+2); ?>" class="page-number-last"><span><?php _e('Nastepna strona'); ?> &rsaquo;&rsaquo;</span></a>
<?php else : ?>
 <a href="<?php echo get_pagenum_link($paged+1); ?>" class="page-number-last"><span><?php _e('Następna strona'); ?> &rsaquo;&rsaquo;</span></a>
<?php endif;  ?>

<?php endif; wp_reset_postdata(); ?>

            </div>

        </section>
</div>

Advertisement

Answer

I managed to fix this issue by counting all published posts and dividing them by number of posts per page (in this case 5 posts):

$count_posts = wp_count_posts()-> publish / 5;

So my code was:

<?php echo previous_posts_link(); ?>
<?php if (!is_paged()):?>
 <a href="<?php echo get_pagenum_link($paged+2); ?>" class="page-number-last"><span><?php _e('Previous page'); ?> &rsaquo;&rsaquo;</span></a>
                <?php elseif ($count_posts > $paged ):?>
                <a href="<?php echo get_pagenum_link($paged+1); ?>" class="page-number-last"><span><?php _e('Next page'); ?> &rsaquo;&rsaquo;</span></a>
                <?php else : ?>
                <?php endif;  ?>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement