我将标准的 Recent Post 小部件复制到 function.php,我注销它并注册我的新类。在小部件中,我看到这个函数负责在 A 标签中显示最近帖子的标题:
<?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 不起作用 - title 总是显示全部。我做错了什么?
最佳答案
您也可以使用 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。
关于Wordpress - 标准小部件中的 the_title() 限制字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27968605/