获取字符串php the_title WordPress中的第一个单词 [英] Get first word in string php the_title WordPress
本文介绍了获取字符串php the_title WordPress中的第一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将wordpress标题缩短为仅第一个单词.对于名为"John Doe"的页面,我想在页面上的某个地方有一个副标题,上面写着"About John",所以我只想从标题中得到第一个单词.
I'm trying to shorten a wordpress title to just the first word. For a page named "John Doe" I want to have a sub title somewhere on the page that says "About John" so I want to just get the first word from the title.
有没有办法用PHP做到这一点?
Is there a way to do this with PHP?
谢谢!
更新:
感谢您的回答!我尝试了以下代码,但似乎无法正常工作.它仍然呼应完整的标题.有什么建议吗?
UPDATE:
Thanks for your answers! I tried the following code but it doesn't seem to be working. It still echoes the full title. Any suggestions?
<?php
$title = the_title();
$names = explode(' ', $title);
echo $names[0];
?>
推荐答案
这很简单:
<?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;
?>
或
<?php
$title = current(explode(' ', get_the_title()));
echo $title;
?>
未经测试,但应该可以:)
untested, but should work :)
希望这会有所帮助:)
这篇关于获取字符串php the_title WordPress中的第一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文