如何通过PHP代码wordpress缩短我的标题长度

时间:2015-04-02 13:04:19

标签: php wordpress title

我想在相关帖子上修剪我的标题长度,我尝试了几种变化而没有人工作。我现在有这个

<p style="margin-top:-4px !important"><a class="title"  href="<?php the_permalink() ?>" title="<?php the_title(); ?>">**<?php echo short_title('...', 3); ?>**</a></p>

</li>
<?php

这个short_title修饰单词(现在我有值3它显示我前3个单词)我想要的字符可能是20,30。我该怎么做?

2 个答案:

答案 0 :(得分:1)

使用substr()

$title = 'some text here for example';
$newTitle =  (strlen($title)>20)?substr($title,0,20):$title;

答案 1 :(得分:0)

更好的方法是不截断字符串,但使用CSS功能来显示...如果是长文本,但保持文本不变,因为对于不同的字体/浏览器/ devivices是不可能得到相同的结果。

css是:

   .long {
      width: 50px;
   }

   .long span {
      padding-top: 4px;
      display: block;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }

HTML:

    <div class="long"><span>Very long text should be truncated</span></div>