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

2019-06-09 01:46发布

I want to get the first letter of a post title in wordpress, then use substr on it to get the first letter by using this snippet but it still displays the full word.

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

2条回答
ら.Afraid
2楼-- · 2019-06-09 02:17

Please try this code maybe useful for you:

<?php 
    $get_title =  get_the_title(); 
    $ltr_group = substr($get_title, 0, 1);
    echo $ltr_group;
?>
查看更多
叼着烟拽天下
3楼-- · 2019-06-09 02:21

It looks like your the_title() function may not be returning a string I would start by adding this to your code to ensure that the $get-title variable is in fact a string.

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

If that doesn't work maybe try:

$ltr_group = substr($get_title, 0, 1);
查看更多
登录 后发表回答