首页 文章

如何在Wordpress中显示the_title的第一个字母?

提问于
浏览
0

我从另一篇文章中得到了这段代码,在这里找到:Get first word in string php the_title WordPress

<?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;
?>

这应该 grab 第一个单词,但如何修改它以显示Wordpress中the_title的第一个字母?

3 回答

  • 3

    echo $first_word[0];

    要么

    echo $title[0];

    或许更有意义的事情:

    echo substring($title, 0, 1);

    请注意,您不能执行 get_the_title()[0] ,因为它是语法错误 .

  • 0

    echo echo $title[0];echo substr($title,0,1);

  • 3
    <?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 //
    
    
    ?>
    

相关问题