How to Add an Anchor Link to WordPress pagination
Posted: May 7th, 2015 | Author: Julia | Filed under: Development, Tips and Tricks | Tags: anchor link, paginate_links, pagination, WordPress | No Comments »There are a few ways to setup WordPress pagination, or ‘paged’ links. I do prefer to work with paginate_links because of set of its arguments, which could be customized according to your needs.
Usage of function ‘paginate links’ is
-
<?php echo paginate_links( $args ); ?>
By default
-
<?php $args = array(
-
'base' => '%_%',
-
'format' => '?page=%#%', // the '%#%' will be replaced with the page number
-
'total' => 1,
-
'current' => 0,
-
'show_all' => False,
-
'end_size' => 1,
-
'mid_size' => 2,
-
'prev_next' => True,
-
'prev_text' => __('« Previous'),
-
'next_text' => __('Next »'),
-
'type' => 'plain',
-
'add_args' => False,
-
'add_fragment' => '',
-
'before_page_number' => '',
-
'after_page_number' => ''
-
); ?>
In case you would like to set up an anchor link so that when pagination is clicked through to the next page it goes to the respective section (and does not to the top of the page) – here is how:
1. Add your unique identifier.
I changed ‘format’=>’?page=%#%’ (which is the page number) to something like ‘format’ => ‘?page=%#%#work’
-
<?php
-
global $wp_query;
-
$big = 999999999; // need an unlikely integer
-
echo paginate_links( array(
-
'base' => str_replace( $big, '%#%#work', esc_url( get_pagenum_link( $big ) ) ),
-
'format' => '?paged=%#%',
-
'current' => max( 1, get_query_var('paged') ),
-
'total' => $wp_query->max_num_pages
-
) );
-
?>
2. Set the name of an anchor.
At the very place which should be the top of your page after clicking through pagination, define the name for your anchor:
-
<a name="work/"> </a>
*Note: Use slash after your anchor name, because usually WordPress URLS end with a slash (and that’s correct).
You are done!
Leave a Reply