I’m using this in my search.php template â¦
<div class="pagination">
<?php echo get_pagination_links(); ?>
</div>
And this is the function â¦
function get_pagination_links() {
global $wp_query;
$big = 999999999;
return paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => false
) );
}
–
The output works: So if I search for “test” and there are more then 10 results and the pagination shows 1 2
where 2
is a link. When clicking page 2
the address bar correctly updates to mysite.com/search/test/page/2
but the resulting page is still number 1
.
There are still the same results as on page number 1
and the pagination links are still the same – 2
is the still the link and 1
is the current page.
Any ideas why that could happen?
Update
Here is where the custom function is called in the template:
<?php
/**
* The template for displaying Search Results pages
*/
get_header(); ?>
<?php if ( have_posts() ) : ?>
<hgroup class="section-heading wrapper">
<h1>Results</h1>
</hgroup>
<section id="results">
<?php while ( have_posts() ) : the_post(); ?>
<ul class="event-items">
<?php get_template_part( 'inc/search', 'result' ); ?>
</ul>
<?php endwhile; ?>
</section>
<div class="pagination tiny wrapper">
<?php echo get_pagination_links(); ?>
</div>
<?php else : ?>
<div class="wrapper">
<h2>Nothing found</h2>
</div>
<?php endif; ?>
<?php get_footer(); ?>
Edit
Using recommended code from the answer below:
/**
* Pagination links for search and archives
*/
function get_pagination_links() {
global $wp_query;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$big = 999999999;
return paginate_links( array(
'base' => @add_query_arg('paged','%#%'),
'format' => '?paged=%#%',
'current' => $current,
'total' => $wp_query->max_num_pages,
'prev_next' => false
) );
}
The pagination is working now however the “permalinks” are not exactly what I want them to be. I use custom_pagination_base()
to have the this permalink structure:
mypage.com/search/term/seite/2
(where seite is the german word for page).
Is it possible to keep the permalinks working like that. The paginaton works with your code, just the permalinks look like this:
mypage.com/search/term?paged=2
I’m fairly certain this is answered elsewhere, but I’ll add it here again.
I believe your issue lies here:
Try this instead:
…then:
Your
'base'
may also be an issue. Instead of this:I use this:
Edit
In case it’s helpful, here’s my entire
paginate_links()
wrapper function:Although this isn’t a solution using your exact code perhaps you can try using this pagination technique as opposed to the example you have used from the WordPress Codec
At a guess I’d say the issue lies with you getting the template part with this line of code:
If this doesn’t solve your issue I’d look at bringing the content from ‘inc/search’ and include it in your search results page without using
<?php get_template_part(); ?>
– it might help you spot any oddities