0

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.

0

1 Answer 1

1

The 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()));
1
  • Oh thank you! That explains that a lot. I suppose I should have caught the whole echo bit from the functions source on WordPress's docu. Oops. Feb 21, 2022 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.