如何在Wordpress中显示the_title的第一个字母?
问题描述:
我已得到另一张贴此代码,发现这里:Get first word in string php the_title WordPress:如何在Wordpress中显示the_title的第一个字母?
<?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;
?>
这应该抓住的第一个字,但我怎么修改它来显示the_title的在WordPress中的第一个字母?
答
echo $first_word[0];
或
echo $title[0];
也许一些更有意义:
echo substring($title, 0, 1);
请注意,你不能做get_the_title()[0]
因为它是一个语法错误。
答
呼应echo $title[0];
或echo substr($title,0,1);
答
<?php
$title = get_the_title();
echo $title[0]; //print first letter
?>
另一种选择
<?php
$title = get_the_title();
echo substr($title,0,1); //takes first character from $title and print //
?>
如果该行导致我应该怎么用,而不是语法错误? – egr103 2012-01-31 21:37:06
任何其他选项.. – Halcyon 2012-01-31 22:03:10
好吧,所以我有点困惑,对不起。那么我的代码会是这样吗? 这是正确的吗? – egr103 2012-02-01 00:29:05