[英]WordPress pagination with custom theme doesn't work
I've a problem with pagination in my WordPress blog, using a custom theme.我在使用自定义主题的 WordPress 博客中遇到分页问题。
This is the loop I actually use to display all posts in the homepage (as you can see, I need to display only posts inserted in Home category):这是我实际用来显示主页中所有帖子的循环(如您所见,我只需要显示插入到主页类别中的帖子):
<!-- Articles -->
<div role="main">
<h2 class="page-title">MOST RECENT</h2>
<hr class="black">
<?php $args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'home',
'order' => 'DESC',
'posts_per_page' => 100,
'paged' => get_query_var( 'paged' ),
'offset' => 1,
);
?>
<?php $arr_posts = new WP_Query( $args ); ?>
<?php if ( $arr_posts->have_posts() ) : ?>
<div id="full-post-list" class="row between-xs">
<?php while ( $arr_posts->have_posts() ) : $arr_posts->the_post(); ?>
<div class="col-xs-12 col-md-5 mansory-card">
<div class="box">
<div class="row middle-xs">
<div class="col-xs-12 col-md-12">
<div class="box">
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail('large', array('class' => 'home-thumb-img')); ?>
</a>
</div>
</div>
</div>
<div class="row middle-xs thumb-home">
<div class="col-xs-12 col-md-12">
<div class="box mansory-text-box">
<span class="mansory-title"><a href="<?php the_permalink() ?>">
<?php echo wp_trim_words( get_the_title(), 5, null ); ?></a></span>
<!-- Funzione PHP per generare numero random di views (mt_rand(1000,2000)) più visite effettive. Da disattivare dopo un mese dal deployment-->
<p class="mansory-details">
<!-- POSTED BY <a class="author-name" href="<?php /* echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><?php the_author(); */?></a> | --><span class="mansory-category">
<?php the_category(', '); ?></span> |
<?php echo get_the_date('F j, Y'); ?>
<!-- | <?php /* echo (mt_rand(1000,2000)) + wpp_get_views(get_the_ID()); */ ?> <i class="far fa-eye"></i> -->
</p>
<p class="mansory-excerpt"><span class="preview-excerpt">
<?php echo get_the_excerpt() ?></span><span class="read-more"><a href="<?php the_permalink() ?>"> Read more</a></span></p>
<hr class="gray">
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div><!-- #Articles -->
The loop display the first 100 posts on the first page, but if I type in the browser mysite.com/page/2 I see the same post on the first page.循环显示第一页上的前 100 篇文章,但如果我在浏览器中输入 mysite.com/page/2,我会在第一页看到相同的文章。
I've read several articles on the web about pagination issues, but I can't find a solution.我在网上阅读了几篇关于分页问题的文章,但找不到解决方案。
Any ideas?有任何想法吗?
Thank you!谢谢!
If removing offset doesnt work try this:如果删除偏移不起作用,请尝试以下操作:
Hope this solves your problem.希望这能解决您的问题。
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts_per_page = 100;
$offset = ( $paged - 1 ) * $posts_per_page;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'home',
'order' => 'DESC',
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'offset' => $offset);
You can add custom pagination您可以添加自定义分页
Step 1- Add function to functions.php files第 1 步 - 将函数添加到 functions.php 文件
if (!function_exists('wpex_pagination' )) {
function wpex_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
//Step-2 Add CSS
ul.page-numbers {
list-style: none;
margin: 0;
}
.page-numbers:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
ul.page-numbers li {
display: block;
float: left;
margin: 0 4px 4px 0;
text-align: center;
}
.page-numbers a,
.page-numbers span {
line-height: 1.6em;
display: block;
padding: 0 6px;
height: 18px;
line-height: 18px;
font-size: 12px;
text-decoration: none;
font-weight: 400;
cursor: pointer;
border: 1px solid #ddd;
color: #888;
}
.page-numbers a span { padding: 0 }
.page-numbers a:hover,
.page-numbers.current,
.page-numbers.current:hover {
color: #000;
background: #f7f7f7;
text-decoration: none;
}
.page-numbers:hover { text-decoration: none }
//Step-3 Add Pagination to your template file //Step-3 将分页添加到您的模板文件中
<?php wpex_pagination(); ?>
offset
parameter has some known issues. offset
参数有一些已知问题。 According to the official documentation ,根据官方文档,
offset (int) – number of post to displace or pass over.
offset (int) – 要替换或传递的帖子数。 Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination.
警告:设置偏移参数会覆盖/忽略分页参数并中断分页。 The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used.
使用 'posts_per_page'=>-1(显示所有帖子)时,将忽略 'offset' 参数。
Following is a workaround proposed by Wordpress.以下是 Wordpress 提出的解决方法。 https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.