Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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);
}
add_filter('next_post_link','add_attr_to_np_links_wpse_92086');
add_filter('previous_post_link','add_attr_to_np_links_wpse_92086');
share|improve this answer
    
Very useful; worked like a charm. And thanks for the info about the depreciated next/previous_post –  Michael Mar 24 '13 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 '13 at 3:17
    
I do appreciate. I've been accused of being crude, so crude code bothers me not. :) –  Michael Mar 24 '13 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/

share|improve this answer
    
Thanks for the information. I will use it in another application. I went with s_ha_dum's approach. –  Michael Mar 24 '13 at 2:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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