如何在wordpress中获取post标题的第一个字母

How to get the first letter of a post title in wordpress

我想在 wordpress 中获取 post 标题的第一个字母,然后在其上使用 substr 通过使用此代码段获取第一个字母,但它仍然显示完整的单词。

<?php 
    $get_title =  the_title(); 
    $ltr_group = mb_substr($get_title, 0, 1);
    echo $ltr_group;
?>

您的 the_title() 函数似乎未返回字符串 我首先将其添加到您的代码中,以确保 $get-title 变量实际上是一个字符串。

echo gettype($get_title), "\n";

如果还是不行,可以试试:

$ltr_group = substr($get_title, 0, 1);

请尝试此代码可能对您有用:

<?php 
    $get_title =  get_the_title(); 
    $ltr_group = substr($get_title, 0, 1);
    echo $ltr_group;
?>