如何修复WordPress自定义文章类型查询中的分页失效问题?
Fixing WP_Query Pagination for Custom Post Type "service-items"
Let's get your pagination working properly for your service page template. The issue stems from a few key oversights with how you're setting up the $paged variable, configuring paginate_links(), and cleaning up after your custom query. Here's the complete fixed code with explanations:
<?php /* Template Name: Service Page */ get_header(); ?> <!-- service Section --> <section id="practice-wrap1" class="section-gray"> <div class="container-fluid"> <?php // Correctly get the current page number $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // Initialize custom WP_Query with proper pagination parameters $query = new WP_Query(array( 'post_type' => 'service-items', 'posts_per_page' => 4, 'paged' => $paged, // Use 'paged' instead of 'page' for post queries 'order' => 'DESC' )); ?> <?php if ($query->have_posts()) : ?> <div class="row text-center"> <?php while ($query->have_posts()) : $query->the_post(); ?> <div class="col-md-3 col-sm-6 col-xs-12"> <div class="service-item"> <div class="service-item-img"> <?php the_post_thumbnail('service-items', array('class' => 'post-thumb')); ?> <div class="overlay-one transition4s"></div> </div> <div class="service-content"> <div class="service-icon"> <span><i class="fa fa-globe"></i></span> </div> <h3 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <p class="description"><?php echo word_count(get_the_excerpt(), '20'); ?></p> <a href="<?php the_permalink(); ?>" class="read-more fa fa-plus"></a> </div> </div> </div> <?php endwhile; ?> </div> <!-- Close the .row containing service items --> <!-- Add pagination with custom query parameters --> <div class="row"> <div class="col-md-12 text-center"> <?php $pagination_args = array( 'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))), 'format' => '?paged=%#%', 'current' => max(1, $paged), 'total' => $query->max_num_pages, 'prev_text' => __('« Previous'), 'next_text' => __('Next »') ); echo paginate_links($pagination_args); ?> </div> </div> <?php else: ?> <div class="row text-center"> <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p> </div> <?php endif; ?> <!-- Reset post data to avoid conflicts with other queries --> <?php wp_reset_postdata(); ?> </div> </section> <?php get_footer(); ?>
Key Fixes Explained:
- Proper
$pagedVariable Setup: We useget_query_var('paged')to pull the current page number, defaulting to1if it doesn't exist (for the first page). This ensures pagination knows which set of posts to load. - Use
pagedInstead ofpage: In WP_Query, the correct parameter for post pagination ispaged—pageis reserved for static Page content. This was a critical mistake in your original code. - Configure
paginate_links(): We pass custom arguments tied to our query:base: Fixes the URL structure for pagination links to work with your page template.format: Defines the query string format for page numbers.current: Sets the active page state correctly.total: Uses$query->max_num_pagesto get the total number of pages from our custom post query.
- Layout Cleanup: Moved pagination outside the service items row and wrapped it in its own row/column to avoid Bootstrap grid layout conflicts.
- Reset Post Data: Added
wp_reset_postdata()after the loop to restore the global post object, preventing issues with other parts of your site (like footer widgets or navigation menus).
内容的提问来源于stack exchange,提问作者Ruhul Amin
相关产品推荐
相关产品推荐

