我有一个带有标题的wordpress网站,如果标题超过50个字符,则需要在标题末尾添加省略号(...),并以50个字符停止标题。以下是我正在编写的PHP,但它似乎无法正常工作,请一位PHP专家来教我正确的方法。任何帮助将不胜感激。
<?php if (strlen("the_title()") > 50) { ?>
<?php the_title(); ?>
<?php } if (strlen("the_title()") < 50) { ?>
<?php echo substr(get_the_title(), 0, 50); ?>...
<?php } ?>
最佳答案
mb_strimwidth函数正是这样做的。
echo mb_strimwidth(get_the_title(), 0, 50, '...');