Wordpress 标题:如果超过 50 个字符,则显示省略号 [英] Wordpress Titles: If Longer Than 50 Characters, Show Ellipsis

查看:26
本文介绍了Wordpress 标题:如果超过 50 个字符,则显示省略号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有标题的 wordpress 网站,如果标题超过 50 个字符,我需要在标题末尾添加一个省略号 (...) 并将标题停止在 50 个字符处.下面是我正在编写的 PHP,但它似乎无法正常工作,寻求 PHP 大师教我正确的方法.任何帮助将不胜感激.

I have a wordpress site with titles, and if the title has more than 50 characters I need to add an ellipsis (...) at the end of the title and stop the title at 50 characters. Below is the PHP I am writing but it seems to not work correctly, seeking a PHP guru to teach me the correct way for this. Any help would be greatly appreciated.

<?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, '...');

这篇关于Wordpress 标题:如果超过 50 个字符,则显示省略号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆