Apply css on last word of post title in WordPress
As we know the wordpress is much popular now a days. Sometimes we need to apply different css to the last word to make it more attractive for our users. Here I am discussing the same situation to make it a little easy with a simple code snippet :
$title = get_the_title(); // get the post title using wordpress default function $title_array = explode(' ', $title); // convert title into an array $last_word = array_pop($title_array); // get the last element of array $modified_last_word = '<span class="last_word">' . $last_word . '</span>'; // adding html span to last word array_push($title_array,$modified_last_word); // pushing modified last word to title array $new_title = implode(' ', $title_array); // convert array in to title string echo $new_title;
Put the above code at your desired location and write css for “” tag with class “last_word”. In this way we can see the title with different css for last word of post title which looks more attractive. In few cases we want to get the last word only then we can use the approach mentioned in my another post here.
Leave a Reply