paginate_links函数'base'与'format'参数异常问题求助
paginate_links() Base & Format Issues in AJAX Requests Hey there! Let's figure out why your base and format parameters aren't working as expected when using paginate_links() in an AJAX request. The root cause here is almost always related to how WordPress resolves pagination URLs in the context of AJAX—since the AJAX endpoint (usually admin-ajax.php) is different from your front-end page's URL, the default base setting ends up pointing to the wrong place.
Here's how to fix it:
1. Correctly Set the base Parameter
You need to explicitly set base to the full URL of the front-end page where your content is displayed (not the AJAX handler URL). This ensures pagination links point back to the right page, even when generated via AJAX.
You can use get_pagenum_link(1) to grab the URL of the first page of your content, then clean it up to remove any existing pagination segments:
// Get the URL for the first page of your content (adjust if using a category archive, etc.) $page_url = get_pagenum_link(1); // Strip any existing "/paged/X" part from the URL $page_url = preg_replace('/\/paged\/\d+/', '', $page_url);
Then use this cleaned URL as the base, appending %_% (the placeholder for the format string):
'base' => trailingslashit($page_url) . '%_%',
2. Adjust the format Parameter
Your existing format: '/paged/%#%' is fine, but make sure it matches your permalink structure. If you're using pretty permalinks, this should work—%#% will be replaced with the page number.
3. Full Modified Code Example
Here's your updated paginate_links() implementation with the fixes:
<?php $big = 999999999; // Unlikely integer for pagination fallback // Get and clean the front-end page URL $page_url = get_pagenum_link(1); $page_url = preg_replace('/\/paged\/\d+/', '', $page_url); $pagination = paginate_links(array( 'mid_size' => 2, 'prev_text' => esc_html__('Previous', 'travel-tour'), 'next_text' => esc_html__('Next', 'travel-tour'), 'current' => max(1, get_query_var('paged')), 'total' => $cat_query->max_num_pages, 'type' => 'array', 'base' => trailingslashit($page_url) . '%_%', 'format' => 'paged/%#%', )); // Output the pagination items if (!empty($pagination)) { echo '<ul class="pagination">'; foreach ($pagination as $page_link) { $active_class = strpos($page_link, 'current') ? 'class="active"' : ''; echo '<li ' . $active_class . '>' . $page_link . '</li>'; } echo '</ul>'; } ?>
4. Front-End AJAX Handling (Critical!)
Even with fixed backend code, you need to prevent the pagination links from triggering a full page reload. Add this JavaScript to handle clicks and fire your AJAX request instead:
jQuery(document).on('click', '.pagination a', function(e) { e.preventDefault(); // Stop default link behavior // Extract the page number from the link URL var pageNumber = jQuery(this).attr('href').split('/paged/')[1]; // Fire your AJAX request jQuery.ajax({ url: ajax_object.ajax_url, // Make sure you've localized this via wp_localize_script() type: 'POST', data: { action: 'your_ajax_action_name', // Replace with your actual AJAX action paged: pageNumber, // Add any other parameters (like category ID) your query needs }, success: function(response) { // Replace your content area with the new paginated content jQuery('.your-content-container').html(response); } }); });
Key Notes:
- Ensure
get_query_var('paged')is correctly passed to your AJAX handler—you'll need to retrieve it in your PHP AJAX callback withget_query_var('paged')or$_POST['paged']. - If you're working with a category archive, replace
get_pagenum_link(1)withget_category_link($cat_query->get_queried_object_id())to get the correct category base URL.
内容的提问来源于stack exchange,提问作者Francis Alvin Tan

