How to add span on WordPress Title for first and third word

If your provided code (as you state) is wrapping the first word of your title with the <span>tag, then the following should also wrap the third word.

$words    = explode( ' ', the_title( '', '',  false ) );
$words[0] = '<span>' . $words[0] . '</span>';
$words[2] = '<span>' . $words[2] . '</span>';
$title    = implode( ' ', $words );
echo $title;

The words in the $words[] array would correspond to the numeric index. [0] being your first word, [1] being your second word, etc.


Edit: To wrap multiple words in your title there are multiple options. Here is the simplest, which involves prefixing the first word in the chain and suffixing the last word in the chain. In this example, we’re wrapping the first, second and third word with a <span> tag.

$words    = explode( ' ', the_title( '', '',  false ) );
$words[0] = '<span>' . $words[0];
$words[2] = $words[2] . '</span>';
$title    = implode( ' ', $words );
echo $title;

To summarize, you are not wrapping each word with an open/close tag pair. Simply open the tag in front of the first word to wrap, and close the tag after the last word.