Je fais une pagination pour ce site Web en utilisant une requête personnalisée et get_next_posts_link , get_previous_posts_link . Le problème est que le lien vers les anciennes entrées ( get_next_posts_link ) ne fonctionne qu’une seule fois, ce qui signifie que si je clique dessus une deuxième fois, cela mènera toujours à la page d’accueil, ce qui est bizarre, car lorsque je vérifie la link, l'attribut href est: http://localhost:8888/athena/event/page/3
.
Il y a 7 pages selon la variable $ queryObject-> max_num_pages
Une petite vidéo de capture d'écran pour montrer ce que je veux dire (27 secondes): https://www.useloom.com/share/f8f9ecac9dd54a4936a913f9c0f5c9f9
Voici mon code:
<!-- section list events-->
<?php
if(get_query_var('paged')){
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$query_args = array(
'post_type' => 'event',
'posts_per_page' => 3,
'paged' => $paged
);
$queryObject = new WP_Query($query_args);
?>
<section class="block-list-events">
<div class="container">
<div style="color: #000;">
<?php var_dump($queryObject->found_posts); ?>
</div>
<div class="list-events">
<?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
<div class="item clearfix">
<div class="img tbl pull-left">
<div class="tbl-cell date">
<p><?php the_time('Y M') ?></p>
<p><span><?php the_time('j') ?></span></p>
</div>
<div class="tbl-cell img-a">
<a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url('event-single'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
</div>
</div>
<div class="info pull-left">
<p class="tag"><?php the_field('label'); ?></p>
<h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
<p class="desc"><?php echo excerpt(25); ?></p>
<div class="button-view-detail">
<a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<div class="clearfix">
<!-- Pagination -->
<?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $queryObject->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries', $queryObject->max_num_pages ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
</div>
</section>
<!-- /end of section list events -->
</main>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
Comme suggéré par @amit, j'ai mis à jour mon code mais le résultat est toujours le même qu'auparavant:
<!-- section list events-->
<?php
if(get_query_var('paged')){
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$query_args = array(
'post_type' => 'event',
'numberposts' => -1,
'posts_per_page' => 3,
'paged' => $paged
);
$queryObject = new WP_Query($query_args);
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $queryObject;
?>
<section class="block-list-events">
<div class="container">
<div style="color: #000;">
<?php var_dump($queryObject->found_posts); ?>
</div>
<div class="list-events">
<?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
<div class="item clearfix">
<div class="img tbl pull-left">
<div class="tbl-cell date">
<p><?php the_time('Y M') ?></p>
<p><span><?php the_time('j') ?></span></p>
</div>
<div class="tbl-cell img-a">
<a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url('event-single'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
</div>
</div>
<div class="info pull-left">
<p class="tag"><?php the_field('label'); ?></p>
<h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
<p class="desc"><?php echo excerpt(25); ?></p>
<div class="button-view-detail">
<a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php
// Reset postdata
wp_reset_postdata();
?>
<div class="clearfix">
<!-- Pagination -->
<?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $queryObject->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
</div>
</section>
<!-- /end of section list events -->
</main>
<?php
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
<?php get_footer(); ?>
<!-- section list events-->
<?php
if(get_query_var('paged')){
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$query_args = array(
'post_type' => 'event',
'numberposts' => -1,
'posts_per_page' => 3,
'paged' => $paged
);
$queryObject = $wp_query;
$queryObject = new WP_Query($query_args);
?>
<section class="block-list-events">
<div class="container">
<div style="color: #000;">
<?php var_dump($queryObject->found_posts); ?>
</div>
<div class="list-events">
<?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
<div class="item clearfix">
<div class="img tbl pull-left">
<div class="tbl-cell date">
<p><?php the_time('Y M') ?></p>
<p><span><?php the_time('j') ?></span></p>
</div>
<div class="tbl-cell img-a">
<a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url('event-single'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
</div>
</div>
<div class="info pull-left">
<p class="tag"><?php the_field('label'); ?></p>
<h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
<p class="desc"><?php echo excerpt(25); ?></p>
<div class="button-view-detail">
<a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<div class="clearfix">
<!-- Pagination -->
<?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $queryObject->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
</div>
<?php
// Reset postdata
wp_reset_postdata();
?>
</section>
<!-- /end of section list events -->
Il est peu probable que la pagination fonctionne avec quoi que ce soit qui ne soit pas la requête principale et il n’ya pas de raison d’essayer de la forcer à fonctionner ailleurs. Utilisez le filtre pre_get_posts
si la modification de la requête principale vous donnera de bons résultats, sinon inventez simplement votre propre système de pagination qui n'utilise pas les mêmes paramètres que wordpress.