views:

27

answers:

1

I want to replace first word in title to have <span></span> inside.

Example for Wordpress title

<h2 class="entry-title"><a href="#">Welcome to Wordpress</a></h2>

I want to be like this

<h2 class="entry-title"><a href="#"><span>Welcome</span> to Wordpress</a></h2>

the function

function span_on_title($span) {
 return preg_replace('', '', $span, 1);
}
add_filter('the_title','span_on_title');

May i know what to put on the preg_replace

+1  A: 
  $title = '<h2 class="entry-title"><a href="#">Welcome to Wordpress</a></h2>';

  $title = preg_replace('/<a([^>]+)>([a-zA-Z]+)\s/i', '<a$1><span>$2</span> ', $title);

  return $title;
Michael Robinson
hmmm.. nothing change when I put the code. I've update `the_title` format with hyperlink
kampit
It does work, you can play with regex here: http://regex.larsolavtorvik.com/
Michael Robinson
@Michael - Not as you wrote it ;) ... You are missing `$title` as the 3rd argument of `preg_replace` ( http://php.net/manual/en/function.preg-replace.php ) - working example ==> http://codepad.org/LHkvfNIf
Peter Ajtai
heh, thanks... slippery eyes missed that one ;)
Michael Robinson
@Michael actually this one worked, but need modify all template which have the `the_title()`. BTW thanks give me a clue. Good example.
kampit
No problem, happy to be able to help :)
Michael Robinson