Reputation: 365
I am trying to wrap a around the first word of multiple word widget titles. I have used the answer from here:
Revised Wordpress function to put a Span around the first word of the Title?
and adjusted it to look like:
function add_label_to_post_title( $title = '' ) {
$ARR_title = explode(" ", $title);
if(sizeof($ARR_title) > 1 ) {
$first_word = "<span>".$ARR_title['0']."</span> ";
unset($ARR_title['0']);
return $first_word. implode(" ", $ARR_title);
} else {
return $title;
}
}
add_filter( 'widget_title', 'add_label_to_post_title' );
and it works great on two word titles. However 3 or more word titles are not affected.
I tried modifying the code with:
preg_replace('/(?<=\>)\b\w*\b|^\w*\b/', '<span>$0</span>', $string);
from here:
wrap <b>-tag around first word of string with preg_replace
and had the same result, works great on two word titles but three or more are not affected. Does anyone have any idea why?
Upvotes: 0
Views: 1303
Reputation: 1385
Please find code snippet using array below
function add_label_to_post_title( $title = '' )
{
$ARR_title = explode(" ", $title);
if(sizeof($ARR_title) > 1 )
{
$ARR_title[0] = "<span>".$ARR_title[0]."</span>";
return implode(" ", $ARR_title);
}
else
{
return $title;
}
}
echo add_label_to_post_title ("hi there this is test code");
Upvotes: 2