The paginate_links WordPress PHP filter allows you to modify the paginated links for your archive pages.
Usage
add_filter('paginate_links', 'your_custom_function'); function your_custom_function($link) { // your custom code here return $link; }
Parameters
$link
(string) – The paginated link URL.
More information
See WordPress Developer Resources: paginate_links
Examples
Adding a CSS class to paginated links
Add a custom CSS class to all paginated links for easy styling.
add_filter('paginate_links', 'add_css_class_to_paginate_links'); function add_css_class_to_paginate_links($link) { $link = str_replace('<a', '<a class="my-custom-class"', $link); return $link; }
Adding a nofollow attribute to paginated links
Prevent search engines from following paginated links by adding a nofollow attribute.
add_filter('paginate_links', 'add_nofollow_to_paginate_links'); function add_nofollow_to_paginate_links($link) { $link = str_replace('<a', '<a rel="nofollow"', $link); return $link; }
Adding an id attribute to paginated links
Add a unique id attribute to each paginated link.
add_filter('paginate_links', 'add_id_to_paginate_links'); function add_id_to_paginate_links($link) { static $counter = 1; $link = str_replace('<a', '<a id="paginate-link-' . $counter . '"', $link); $counter++; return $link; }
Removing the href attribute from the current page link
Disable the link for the current page in the pagination by removing the href attribute.
add_filter('paginate_links', 'remove_href_from_current_page'); function remove_href_from_current_page($link) { global $paged; if (strpos($link, 'current') !== false) { $link = str_replace(' href=', ' data-', $link); } return $link; }
Adding a data attribute to paginated links
Add a custom data attribute to all paginated links for usage with JavaScript or other purposes.
add_filter('paginate_links', 'add_data_attribute_to_paginate_links'); function add_data_attribute_to_paginate_links($link) { $link = str_replace('<a', '<a data-custom-attr="my-value"', $link); return $link; }