Adding Post Pagination Attributes in WordPress

Be sure to checkout this comment to see why adding title attributes may not be a good idea.

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:

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.

Pagination Title Attribute

Pagination Title Attribute – notice that you can recognize it on hover.

This has benefits for both semantic reasons as well as [supposed] 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.

10 Comments

This has benefits for both semantic reasons as well as [supposed] 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.

That’s about right.

While I can’t speak to the SEO benefits, I’d encourage you to reconsider this practice as title tags aren’t accessible at all and might even be bad (mostly redundant) in some cases for screen reader users.

The WordPress Accessibility Team has recently been moving to aggressively remove the title attribute from all over WordPress for this same reason.

Good article, as always, and great comments (I’m now thinking I need to review some stuff in my themes); but, the color scheme for your code sample has very difficult to read parts with light grey on lighter grey (coincidentally segueing into accessibility as well).

Trackbacks and Pingbacks

[…] post that walks you through setting up more advanced pagination in […]

[…] post that walks you through setting up more advanced pagination in […]

[…] post that walks you through setting up more advanced pagination in […]

[…] post that walks you through setting up more advanced pagination in […]

Leave a Reply

Name and email address are required. Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>