我从另一篇文章中得到了这段代码,在这里找到:Get first word in string php the_title WordPress:
<?php $title = get_the_title(); // This must be!, because this is the return - the_title
would be echo
$title_array = explode(' ', $title);
$first_word = $title_array[0];
echo $first_word;
?>
这应该 grab 第一个单词,但如何修改它以显示Wordpress中the_title的第一个字母?
3 回答
echo $first_word[0];
要么
echo $title[0];
或许更有意义的事情:
echo substring($title, 0, 1);
请注意,您不能执行
get_the_title()[0]
,因为它是语法错误 .echo
echo $title[0];
或echo substr($title,0,1);
另外一个选项