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();
?>