最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

templates - Can't split the_title() by white space

programmeradmin2浏览0评论

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.

Share Improve this question edited Feb 13, 2022 at 22:16 WASasquatch asked Feb 13, 2022 at 21:20 WASasquatchWASasquatch 1032 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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()));
发布评论

评论列表(0)

  1. 暂无评论