admin管理员组文章数量:1404332
I am working on a custom loop using WP_Query using the offset parameter. The problem is that as soon as I add the offset, it breaks the pagination, displaying the same links no matter the page number.
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'notice',
'posts_per_page' => 6,
'offset' => 1,
'paged' => $paged
);
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h3>
<?php endwhile; ?>
<?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
<?php endif; ?>
I tried following the code here:
But adding this code to my theme breaks any related queries.
Any ideas on how I could make this work?
I am working on a custom loop using WP_Query using the offset parameter. The problem is that as soon as I add the offset, it breaks the pagination, displaying the same links no matter the page number.
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'notice',
'posts_per_page' => 6,
'offset' => 1,
'paged' => $paged
);
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h3>
<?php endwhile; ?>
<?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
<?php endif; ?>
I tried following the code here:
https://codex.wordpress/Making_Custom_Queries_using_Offset_and_Pagination
But adding this code to my theme breaks any related queries.
Any ideas on how I could make this work?
Share Improve this question edited Mar 26, 2017 at 16:58 Johann asked Mar 26, 2017 at 16:20 JohannJohann 8674 gold badges14 silver badges31 bronze badges1 Answer
Reset to default 1I had the same issue.
Check out if this works for you:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = 9;
$default_offset = 4;
if ($paged == 1) {
$offset = $default_offset;
} else {
$offset = (($paged - 1) * $per_page) + $default_offset;
}
$args = array(
'post_type' => 'post',
'posts_per_page' => $per_page,
'order' => 'DESC',
'offset' => $offset,
'paged' => $paged
);
$loop = new WP_Query($args);
while ($loop->have_posts()) :
$loop->the_post();
?>
... HTML
<?php
endwhile;
wp_pagenavi(array('query' => $loop)); wp_reset_postdata();
?>
本文标签: WP Query with offset breaks wppagenavi or any pagination
版权声明:本文标题:WP Query with offset breaks wp_pagenavi or any pagination 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744799610a2625784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论