为自定义WordPress主题博客页的foreach循环添加分页
Got it, let's get that pagination working for your custom blog page! Since you're using get_posts() with a foreach loop instead of the standard WordPress loop, you just need to explicitly add the paged parameter to your post query and set up the pagination links correctly. Here's how to do it step by step:
Step 1: Get the Current Page Number
First, grab the current page number from the URL (WordPress uses the paged query variable for this). If it's not set (like on the first page), default to 1:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
Step 2: Modify Your get_posts() Call
Update your get_posts() function to include both the paged parameter and posts_per_page (so WordPress knows how many posts to show per page). Adjust the posts_per_page value to match your desired count:
$posts = get_posts(array( 'posts_per_page' => 10, // Change this to your preferred number 'paged' => $paged ));
Step 3: Keep Your Existing Loop (With a Small Fix)
Your foreach loop can stay mostly the same, but don't forget to reset post data at the end to avoid conflicts with other queries on the page:
foreach ($posts as $post) : setup_postdata($post); // Your existing post content code here (title, excerpt, etc.) the_title(); the_excerpt(); // ... rest of your post display code endforeach; wp_reset_postdata(); // Important: Reset post data after the loop
Step 4: Add Pagination Links
The most reliable way for custom queries is to use paginate_links(), which gives you full control over the output. First calculate the total number of pages, then generate the links:
// Calculate total pages $total_posts = wp_count_posts()->publish; $total_pages = ceil($total_posts / 10); // Match this to your posts_per_page value // Output pagination echo paginate_links(array( 'current' => $paged, 'total' => $total_pages, 'prev_text' => __('« Previous'), 'next_text' => __('Next »'), 'mid_size' => 2, // Number of pages to show before/after current page ));
Alternative: Using Default Previous/Next Links
If you prefer the simpler previous_posts_link() and next_posts_link(), temporarily override the main query's max_num_pages value to make them work with your custom query:
global $wp_query; $wp_query->max_num_pages = $total_pages; previous_posts_link('« Newer Posts'); echo ' | '; next_posts_link('Older Posts »');
Full Working Code Example
Putting it all together, your code would look like this:
<?php if (is_page('Blog')) : ?> <?php // Get current page number $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // Get posts with pagination $posts = get_posts(array( 'posts_per_page' => 10, 'paged' => $paged )); foreach ($posts as $post) : setup_postdata($post); ?> <!-- Your post content here --> <h2><?php the_title(); ?></h2> <div><?php the_excerpt(); ?></div> <!-- End post content --> <?php endforeach; wp_reset_postdata(); ?> <?php // Calculate total pages for pagination $total_posts = wp_count_posts()->publish; $total_pages = ceil($total_posts / 10); // Display pagination links echo paginate_links(array( 'current' => $paged, 'total' => $total_pages, 'prev_text' => __('« Previous'), 'next_text' => __('Next »'), )); ?> <?php endif; ?>
Just adjust the posts_per_page value and post content markup to match your theme's design, and you'll have working pagination!
内容的提问来源于stack exchange,提问作者Chris Gatherer

