In this guide, I will show you how to remove trailing slash in pagination links on your WordPress theme. This is quick and easy if you have access to your theme code and are able to modify it.
By default WordPress adds a trailing slash on the pagination links as seen in the screen shot below. This is a fresh site using the WordPress “Twenty Twenty-Two” theme and some dummy data. I prefer doing examples like this with the least modified site / code possible to make it easier to follow the instructions and get a successful result.
What is a trailing slash?
A trailing slash is when a URL ends with a slash. For example,
https://example.com/contact/ ends with a trailing slash at the end.
An example of not ending without a trailing slash for this same URL would be
Why remove trailing slash from the pagination link in WordPress pagination?
You might ask yourself, why would I want to remove the trailing slash from the pagination links and that is a very valid question. In my opinion, it all comes down to consistency of the site and your preference. When creating a site from scratch you can choose to end with a trailing slash or not.
By default, WordPress will always use the trailing slash for all of the dynamically generated links in the site. You can override this by changing the settings in permalinks. If you would like to learn more about this, drop a comment below and I can write an article on it.
Before you change the trailing slash
Remove trailing slash in pagination link in WordPress
To remove the trailing slash from the pagination in WordPress it is as simple as putting this one liner in you
Note: If the file does not exist, you should be able to create it in the root of your active theme directory.
// Remove trailing slash from pagination links
add_filter( 'get_pagenum_link', 'untrailingslashit');
We now see that the pagination link does not include a trailing slash! I told you it was going to be easy! So what just happened here? We can break it down as follows.
add_filter We use a WordPress hook that hooks into the internal data at runtime and allows us to make modifications before it is returned to the page. It takes two input parameters. 1) Is the hook that we are trying to tap into, and 2) The callback function we want to run when the hook is called.
paginate_linkIt specifically allows us to modify paginated number links for the given archive pages.
get_pagenum_linkIt specifically allows us to change the
previouslinks for the archive pagination.
untrailingslashit We pass it as the callback function when the filter is called.
untrailingslashit, which is a WordPress function, will remove a trailing backslash or forward slash if present.
As you can see it was easy to remove trailing slash in pagination link within WordPress. WordPress pagination is a very powerful feature and making sure that you have it set up correctly is very important to keep your site consistent. What is your preference for the trailing slash on your website? Have you modified your WordPress Pagination? Drop a comment below to get the conversation started.
If you would like to suggest a new article or video to be released, please reach out via the Contact Page. I look forward to hearing your feedback and providing content that is helpful for you!