最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Pagination on template page for custom query redirecting to index.php

programmeradmin2浏览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>

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>
Share Improve this question asked Feb 27, 2022 at 2:05 jPark197jPark197 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论