我有以下代码:
<center>
<a href="<?php the_permalink(); ?>"
<span style="color: #ff000a; text-align: center;">
<strong><?php the_title(); ?></strong>
</span>
</a>
the_title();
会告诉我姓名和姓氏。
我想将the_title();
分开来显示第一个单词,所以只是名字而不是姓氏。
尝试了一切,但我没有取得任何成就。
答案 0 :(得分:0)
您应首先检索您的标题。使用get_the_title();或the_title()
the_title('', '', false);
!important ,您应将第一个和第二个参数设置为空字符串,将第三个参数设置为false,如下所示 - {{ 1}}但我建议你使用get_the_title()。然后你需要将你的字符串分成带有单词的部分。函数the_title();会对您有所帮助。
举一个例子:
// exmplate title = 'John Smith';
$words = explode(' ', get_the_title());
print_r($words); // will print array like this:
array(
0 => 'John',
1 => 'Smith',
)
答案 1 :(得分:0)
正如其他人指出的那样,您可能需要get_the_title
,因为它会返回数据而不是回显数据。 <?=
是<?php echo
的缩写:
list($first, $last) = explode(' ', get_the_title());
<?=$first ?>
<span style="color: #ff000a; text-align: center;">
<strong><?=$last ?></strong>
</span>
如果get_the_title
由于某种原因无效,请传递false
以便返回数据而不会回显:
list($first, $last) = explode(' ', the_title('', '', false));
这也只是输出名字:
echo explode(' ', the_title('', '', false))[0];