首页 文章

Wordpress - 标准小部件中的the_title()限制字符

提问于
浏览
1

我将标准的Recent Post小部件复制到function.php,我取消注册并注册我的新类 . 在小部件中,我看到这个函数负责显示A标签中最近帖子的 Headers :

<?php while ( $r->have_posts() ) : $r->the_post(); ?>

        <li>
            <a href="<?php the_permalink(); ?>">
                <?php  if ( get_the_title() ) {
                    $t = the_title();
                    $t = substr($t, 0, 40); /// this doesn't work

                }else{
                    the_ID();
                }
                ?>
            </a>
...
...

但是这个substr不起作用 - Headers 总是全部显示 . 我做错了什么?

2 回答

  • 2

    您可以使用mb_substr(),它的工作方式与substr几乎相同,但区别在于您可以添加新参数来指定编码类型,无论是UTF-8还是不同的编码 .

    试试这个:

    $t =  mb_substr($t, 0, 40, 'UTF-8');
    

    后期编辑:改变

    $t = the_title();
    

    $t = get_the_title();
    

    您正在使用the_title而不是get_the_title将其提供给特定变量 . 并且一定要在所有这些之后回复$ t .

  • 0

    这一个应该工作

    echo mb_strimwidth(get_the_title(), 0, 40, '...');
    

相关问题