I am trying to build up a custom archive page. However, I would like this archive page to skip the first 5 posts.
I’ve been able to accomplish this with the offset property. However, the pagination seems to break.
Here’s my code before the while:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=".get_option("posts_per_page').'&paged=' . $paged);
?>
And this is the code I’m using for the pagination:
<?php wp_reset_postdata(); ?>
<div class="nav-previous" style="margin-bottom:24px; font-size:1.3em;"><?php next_posts_link(__('<span class="meta-nav">«</span> Entradas anteriores', '')) ?></div>
<div class="nav-next" style="margin-bottom:24px; font-size:1.3em;"><?php previous_posts_link(__('Entradas recientes <span class="meta-nav">»</span>', '')) ?></div>
Any help is deeply appreciated.
Johann
1 Answer
Of course it breaks. Pagination depends on the main query saved in the $wp_query
object and you have overwritten that. When the next page load the ordinary wp_query
that you have not overwritten will run and it won’t match the one you’ve altered.
Create a filter for pre_get_posts
to alter the main query.
function pregp_wpse_97354($qry) {
// control where this runs
if (is_main_query() && $qry->is_archive()) {
$qry->set('offset',5);
}
}
add_action('pre_get_posts','pregp_wpse_97354');
Notice // control where this runs
. The line immediately following determines where this filter runs. I’d don’t enough about your setup to get that right so I made something up. I am sure that that needs to be more specific.