The following code

<?php previous_post('%', '&#60;', 'no'); ?>
<?php next_post('%', '&#62; ', 'no'); ?>

returns these links, as it should

<a href="post_address/"><</a>
<a href="post_address/">></a>

My question is about adding a class and title attribute so that the code returns

<a class="normalTip" title="Previous Project" href="post_address/"><</a>
<a class="normalTip" title="Next Project" href="post_address/">></a>

I am using the iWR Tooltip plugin and would like to use it on the links for the post navigation.

There is probably a simple solution by adding something to the function.php file, but I have not come up with the answer. Any ideas?

2 Answers 2


previous_post and next_post are deprecated. you should be using previous_post_link and next_post_link. That function applies the {$adjacent}_post_link filter. You can use that to get what you want. A very, very crude example:

function construct_np_attr_wpse_92086($matches) {
  if ('prev' == $matches[1]) {
    $np = 'Previous';
  } else {
    $np = 'Next';
  $ret = 'rel="'.$matches[1].'" class="normalTip" ';
  if (!empty($np)) {
    $ret .= 'title="'.$np.' Project"';
  return $ret;
function add_attr_to_np_links_wpse_92086($link) {
  return preg_replace_callback('/rel="([^"]+)"/','construct_np_attr_wpse_92086',$link);
  • Very useful; worked like a charm. And thanks for the info about the depreciated next/previous_post
    – Michael
    Mar 24, 2013 at 2:06
  • Glad it works, but it is crude. That is a very simple regex. Watch it carefully and tweak as needed.
    – s_ha_dum
    Mar 24, 2013 at 3:17
  • I do appreciate. I've been accused of being crude, so crude code bothers me not. :)
    – Michael
    Mar 24, 2013 at 12:12

jQuery makes it pretty easy to add a class to an element in the DOM. The code looks like this:

$("p").addClass("myClass yourClass");

Pretty simple, eh? You can see more of the jQuery documentation here: http://api.jquery.com/addClass/

  • Thanks for the information. I will use it in another application. I went with s_ha_dum's approach.
    – Michael
    Mar 24, 2013 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.