How to get last word of post title in WordPress
Sometimes we need to get the last word from title of our post. We want the last word as per our requirement in few rare cases. You can use below mentioned code directly to the place where you want to print the last word of post title :
$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 echo $last_word; // print the last word
In this way we can easily get the last word of post title. Sometimes we need to Apply css on last word of post title in wordpress then we need to apply any additional html tags to this last word and and then again push this to the array of post title. To do the same please check my another post here.
Here I have prepared this code only for title, we can modify this as per our needs like we can do this for content etc.
Leave a Reply