I m using WP query to show my content. It's a custom post type call "enseignement". Query works, but if I want to implement the pagination, that doesn't work. The URL is tranformed in page/2/ but nothing is displayed.
My code
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'enseignement',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'cycle', // name of custom field
'value' => $_GET['cycle'], // matches exactly "red"
'compare' => 'LIKE',
),
array(
'key' => 'lieu',
'value' => $_GET['lieu'],
'compare' => 'LIKE',
),
),
);
$loop = new WP_Query( $args ); ?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
<?php get_template_part( 'content', 'enseignement', get_post_format() );?>
<?php endwhile; ?>
<?php
next_posts_link( 'Older Entries', $loop->max_num_pages );
previous_posts_link( 'Next Entries »' );
wp_reset_query();
?>
<?php endif; ?>
Can you help me please ?
Thank you !
I'm using this function, it works 100%. Adapt yourself and will be ok.
<?php
function generatePagination($paged, $loop, $echo = TRUE) {
$big = 999999999; // need an unlikely integer
$pages = paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, $paged),
'total' => $loop->max_num_pages,
'type' => 'array',
'total' => $loop->max_num_pages,
'prev_next' => TRUE,
'prev_text' => '<span aria-hidden="true">«</span>',
'next_text' => '<span aria-hidden="true">»</span>'
));
if (is_array($pages)) {
$prevText = '<li class="paginated_link disabled"><span aria-hidden="true">«</span></li>';
$nextText = '<li class="paginated_link disabled"><span aria-hidden="true">»</span></li>';
$pagination = '<ul class="pagination">';
if ($paged == 0 || $paged == 1) {
$pagination .= $prevText;
}
foreach ($pages as $page) {
$pagination .= '<li class="paginated_link';
if (strpos($page, 'current') !== false) {
$pagination .= ' active';
}
$pagination .= '">' . $page . '</li>';
}
if ($loop->max_num_pages == $paged) {
$pagination .= $nextText;
}
$pagination .= '</ul>';
if ($echo) {
echo $pagination;
} else {
return $pagination;
}
}
}
?>