I am having issues splitting the_title()
by white space in my template.
$title_split = explode(" ", trim(the_title()));
and (from here: )
$words = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);
(To be clear I'm not using preg as was shown but customized for my use, it was just the first thing I found when looking for a "proper")
Both produce an empty array on the phrase 2022 Theme Challenge
.
I have also made sure the output uses white spaces, which it does. The template produces:
<h2 class="post-title">2022 Theme Challenge</h2>
I am trying to replace the first word in a title with encapsulating <span>
tags to style it greeen.
I am having issues splitting the_title()
by white space in my template.
$title_split = explode(" ", trim(the_title()));
and (from here: https://stackoverflow.com/questions/23206953/split-string-by-white-space)
$words = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);
(To be clear I'm not using preg as was shown but customized for my use, it was just the first thing I found when looking for a "proper")
Both produce an empty array on the phrase 2022 Theme Challenge
.
I have also made sure the output uses white spaces, which it does. The template produces:
<h2 class="post-title">2022 Theme Challenge</h2>
I am trying to replace the first word in a title with encapsulating <span>
tags to style it greeen.
1 Answer
Reset to default 1The first way you tried should work if you use get_the_title()
instead. It’s because get_the_title returns the title, whereas the_title()
function echos the title by default.
$title_split = explode(" ", trim(get_the_title()));