skip to Main Content

This is my code.

 <?php
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $next_args = array(
                    'post_type' => 'articles',
                    'post_status' => 'publish',
                    'posts_per_page' => 3, // post per page
                    'paged' => $paged,
                );
                $next_the_query = new WP_Query($next_args);
                if ($next_the_query->have_posts()) :
                ?>

                    <?php while ($next_the_query->have_posts()) : $next_the_query->the_post(); ?>
                        <h3><?php the_title() ?></h3>
                    <?php endwhile; ?>

                    <div class="pagination">
                    <?php
                    $total_pages = $next_the_query->max_num_pages;

                    if ($total_pages > 1){
                
                        $current_page = max(1, get_query_var('paged'));
                
                        echo paginate_links(array(
                            'base' => get_pagenum_link(1) . '%_%',
                            'format' => '/page/%#%',
                            'current' => $current_page,
                            'total' => $total_pages,
                            'prev_text'    => __('« prev'),
                            'next_text'    => __('next »'),
                        ));
                    }
                    ?>
                </div>

                <?php endif; ?>

I get links like this, 1 2 3 4 next ».

But unfortunately when I click a link I get 404 not found error

This is my site original link,

www.mysite.com/blog/

and getting 404 for www.mysite.com/blog/page/2 link

2

Answers


  1. Chosen as BEST ANSWER

    Finally, I found an answer and adding it here for future users,

    I had to change query parameter from 'paged' to 'page'.

    <?php
        $page = (get_query_var('page')) ? get_query_var('page') : 1;
        $next_args = array(
             'post_type' => 'articles',
             'post_status' => 'publish',
             'posts_per_page' => 3, // post per page
             'paged' => $page,
        ); 
    
               
    

    Here also I changed 'paged' to 'page'. And changed format like 'format' => '?page=%#%', And also kept 'base' option of paginate_links array as default

    $current_page = max(1, get_query_var('page'));
    
    echo paginate_links(array(
        'format' => '?page=%#%',
        'current' => $current_page,
        'total' => $total_pages,
        'prev_text'    => __('« prev'),
        'next_text'    => __('next »'),
    ));
    

  2. Log in to WordPress as the administrator
    Under Dashboard, click Settings, and then click Permalinks. The Permalink Settings page appears.
    Under Common Settings, select the permalink structure that you want to use for your pages and posts.

    more information: https://wordpress.org/support/article/settings-permalinks-screen/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search