I don’t quite understand how the rewriting of pagination_links()
works. Here is my pagination function :
$big = 999999999;
$translated = __( 'Page ', 'epc' );
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?page=%#%', // Here I'd like to add my anchor #result
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $wp_query->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>',
'prev_text' => __( 'Précédent', 'epc' ),
'next_text' => __( 'Suivant', 'epc' ),
'type' => 'list',
)
);
However, I have pretty permalink turned on so the format ?page=%#%
is changed to /page/#/
. So far so good. But I have two problems :
-
First, my query has two variables in the permalink like so
?programme=10&type=2
. When I go on the second page, wordpress adds#038;type=2
at the end of the permalink (now I have?programme=10&type=2#038;type=2
). I don’t know why. -
Second, if I try to add an anchor to the format like so
?page=%#%#result
it will not work. If I try to rename the variable to?nb=%#%#result
it’s still not working. I even tried/page/#/#result
as it fits the pretty permalink structure but still no luck. Can someone put me on the right track or explain what is happening here?
Thanks!
UPDATE 1
Thanks to @bonger comment, the permalink works perfectly with this updated code:
$big = 999999999;
$translated = __( 'Page ', 'epc' );
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big, false ) ),
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $wp_query->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>',
'prev_text' => __( 'Précédent', 'epc' ),
'next_text' => __( 'Suivant', 'epc' ),
'type' => 'list',
'add_fragment' => '#result',
) );
However, after digging a little bit more, when clicked, the previous link looses the anchor in Safari only (works fine in Chrome and Firefox). I’ve read this old post and it seems that there is something weird with how Safari handles the redirect.
Right know, my previous link displays this href http://mywebsite.com/page/1/#result
or http://mywebsite.com/page/1/?programme=10&type=2#result
depending on the filtering. However, when I click on it, it redirects to http://mywebsite.com/
or http://mywebsite.com/?programme=10&type=2
. Apparently, Safari needs the /
before the #anchor
but I am not sure how to add this as if I add it to the fragment
arg, it gets escaped. Any ideas?