[英]Wordpress custom pagination links to a blank page
this is my archive of posts: https://polnapol-tarnow.pl/aktualnosci/page/4/这是我的帖子存档: 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").只有 4 个页面,但仍然显示到另一个(空白)页面的链接(“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'); ?> ››</span></a>
<?php else : ?>
<a href="<?php echo get_pagenum_link($paged+1); ?>" class="page-number-last"><span><?php _e('Next page'); ?> ››</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 w kuchni</h2>
<p>nieustannie badamy otaczający świat aby tworzyć nie tylko lepsze potrawy, ale i klimat naszego włoskiego lokalu. Inspirujemy się i piszemy o 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'); ?> ››</span></a>
<?php else : ?>
<a href="<?php echo get_pagenum_link($paged+1); ?>" class="page-number-last"><span><?php _e('Następna strona'); ?> ››</span></a>
<?php endif; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</section>
</div>
You should use the pagination of WordPress.您应该使用 WordPress 的分页。 Go to this link: WordPress pagination
Go 到这个链接: WordPress 分页
You can use this template for your pagination:您可以使用此模板进行分页:
<div class="paginate">
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '<<' ,
'next_text' => '>>' ,
) );
?>
</div>
And you can set a number of the post in one page by going WordPress panel > setting > reading > blog page show at most.您最多可以通过 WordPress 面板 > 设置 > 阅读 > 博客页面显示在一页中设置多个帖子。
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):我设法通过计算所有已发布的帖子并将它们除以每页的帖子数(在本例中为 5 个帖子)来解决此问题:
$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'); ?> ››</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'); ?> ››</span></a>
<?php else : ?>
<?php endif; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.