0

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.

  • Have you tried saving your permalinks. Please go to Settings > Permalinks in WP Admin and click Save Changes – Sneha Agarwal May 16 at 11:42
  • Possible duplicate of WordPress /page/2 404 pagination problem - what to do? – leymannx May 16 at 12:08
  • Here's a good read as well: stackoverflow.com/questions/42189247/…. On what page or post exactly are you displaying the results of your custom query? You may need to adjust the main query via 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. – leymannx May 16 at 12:10
  • I've tried it before, it does not give me any solution. – Dario B. May 16 at 12:24
0

I 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.