Han*_*com 2 php string wordpress title
我试图将wordpress标题缩短为第一个单词.对于名为"John Doe"的页面,我想在页面的某处显示一个"关于约翰"的子标题,所以我想从标题中获取第一个单词.
有没有办法用PHP做到这一点?
谢谢!
更新:
谢谢你的回答!我尝试了以下代码但它似乎没有工作.它仍然与完整的标题相呼应.有什么建议?
<?php
$title = the_title();
$names = explode(' ', $title);
echo $names[0];
?>
Run Code Online (Sandbox Code Playgroud)
这很容易:
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
要么
<?php
$title = current(explode(' ', get_the_title()));
echo $title;
?>
Run Code Online (Sandbox Code Playgroud)
未经测试,但应该工作:)
希望这有用:)
$first_word = current(explode(' ', $title ));
Run Code Online (Sandbox Code Playgroud)
或在您的模板文件中
<?php echo current(explode(' ', $title )) ?>
Run Code Online (Sandbox Code Playgroud)
通过空间爆炸并获得结果数组中的第一个元素