0

I'm building my first theme and have a template page "page-blog" with a custom wp_query. I've tried adding pagination to the query but so far the pagination redirects to the index page. As expected the url structure shows /blog/page/2.

What am I doing wrong here? Do I need to just create my own pagination method?

<div class="blog-listings">
        <?php
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'post_type' => 'blog',
                'post_status' => 'publish',
                'posts_per_page' => '2',
                'paged' => $paged,
                'orderby' => 'date',
            );
            $blog_loop = new WP_Query( $args );
            if ($blog_loop->have_posts()) : 
                while ($blog_loop->have_posts()) : $blog_loop->the_post();
                    include('template-parts/content/content-single-blog.php');
                endwhile;
                $total_pages = $blog_loop->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'),
                    ));
                }    
                wp_reset_postdata();
            endif;
        ?>
    </div>

1 Answer 1

1

Ok the issue was that the page slug was the same name as the post type. Changing the post type from 'blog' to 'blogposts' solved it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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