I am using pagination in wordpress, the truth is that I have used it several times. But this time, when I access page number 2, I get an error 404. I've searched a bit and I've tried several functions, but not work. Here I leave my loop
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news = array(
'post_type' => 'post',
'category_name' => 'projekte',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => 8
);
$wp_query = new WP_Query($news);
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div class="column is-one-quarter">
<a href="<?php the_permalink() ?>">
<div class="thecontent">
<div class="image-container">
<?php the_post_thumbnail(); ?>
</div>
<div class="post-container">
<div class="subtitle">
<?php the_title(); ?>
</div>
<div class="contain">
<p><?php the_field('beschreibung'); ?></p>
</div>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
<div class="pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '/page/%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages,
'show_all' => true,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => __(''),
'next_text' => __(''),
'type' => 'plain',
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
));
?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
I hope someone can help me, because I'm under pressure and I feel blocked. Thanks.
I am using pagination in wordpress, the truth is that I have used it several times. But this time, when I access page number 2, I get an error 404. I've searched a bit and I've tried several functions, but not work. Here I leave my loop
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news = array(
'post_type' => 'post',
'category_name' => 'projekte',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => 8
);
$wp_query = new WP_Query($news);
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div class="column is-one-quarter">
<a href="<?php the_permalink() ?>">
<div class="thecontent">
<div class="image-container">
<?php the_post_thumbnail(); ?>
</div>
<div class="post-container">
<div class="subtitle">
<?php the_title(); ?>
</div>
<div class="contain">
<p><?php the_field('beschreibung'); ?></p>
</div>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
<div class="pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '/page/%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages,
'show_all' => true,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => __(''),
'next_text' => __(''),
'type' => 'plain',
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
));
?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
I hope someone can help me, because I'm under pressure and I feel blocked. Thanks.
Share Improve this question edited May 16, 2019 at 12:23 Dario B. asked May 16, 2019 at 10:20 Dario B.Dario B. 15510 bronze badges 4 |1 Answer
Reset to default 0I found the solution.
First, in functions.php:
function mg_news_pagination_rewrite() {
add_rewrite_rule(get_option('category_base').'/page/?([0-9]{1,})/?$', 'index.php?
pagename='.get_option('category_base').'&paged=$matches[1]', 'top');
}
add_action('init', 'mg_news_pagination_rewrite');
and then in wordpress, settings> Permalinks changes must be saved
pre_get_posts
, so that WordPress globally knows aha oho what query exactly is running on that page right now, will it have pages or should I trigger a 404 as not every page should simply be accessible with a*/page/*
URL. – norman.lol Commented May 16, 2019 at 12:10