When it comes to working with pagination in WordPress, it’s relatively easy to manage:
- There is pagination for index and archive pages.
- There is pagination at the single post level.
Each of which usually consists of doing the following:
- If you’re on an index or an archive page then you’ll use `get_next_posts_link`, `next_posts_link`, `get_previous_posts_link`, `previous_posts_link`.
- If you’re on a single post page, then you’ll use `get_previous_post_link`, `previous_post_link`, `get_next_post_link`, and `next_post_link`.
Enough enough, right?
But there are times in which you may want to add or enhance some of the attributes that are on each of these anchors.
Post Pagination Attributes in WordPress
If you’re interested in adding post pagination attribution attributes in WordPress specifically for single post pages, it’s relatively easy to do given that you know the proper hooks for doing so.
Case in point, whenever I’m working on a theme, I like to add a `title` attribute to my single post anchors such that the pagers not only display the post title of the previous and next post, but also that it adds a `title` attribute reading the same.
This has benefits for both semantic reasons as well as SEO reasons.
To be honest, I’m not expert in SEO – but I do care about semantics – if this does positively impact, then all the better, but the thing is that SEO is basically a synonym for “what looks good to Google” and that changes from time to time.
Regardless, if you’re looking to introduce the post title to each of your single post paginated anchors, here are the filters and the code for how to do it:
The code should be relatively self-explanatory, but here’s the general flow of control:
- If the link has the ‘rel=”prev”` attribute, then we’re dealing with the previous link. If that’s the case, then we’ll grab the previous posts title.
- Otherwise, we’re dealing with the next post, so we’ll grab the title for the post that comes after this.
If, of course, there’s no other previous post or no other next post, then the attribute will not be written out and the default anchor will be returned.