我做错了什么?
$title = get_the_title();
$firstLetter = $title[0];
$title[0] = '<span class = "wrapBlue">' . $firstLetter . '</span>';
echo $title; // comes out with weird switched around string?
get_the_title()
是一个wordpress函数。
var_dump on $title
给出一个长度为21的字符串。
var_dump on $firstLetter
给出一个包含长度为1的正确字符的字符串
答案 0 :(得分:5)
如您所知$title[0]
指的是第一个字母 - 但您要分配的内容不是字母。尝试这样的事情:
$title = '<span class = "wrapBlue">' . $firstLetter . '</span>' . substr($title, 1);
答案 1 :(得分:0)
试试这个:
$title = get_the_title();
$firstLetter = substr($title, 0, 1);
$title .= '<span class = "wrapBlue">' . $firstLetter . '</span>' . substr($title, 1, strlen($title);
echo $title;
Oneliner:
echo '<span class = "wrapBlue">' . substr(get_the_title(), 0, 1) . '</span>' . substr(get_the_title(), 1, strlen(get_the_title());