【问题标题】:Trim character display length修剪字符显示长度
【发布时间】:2010-11-24 22:48:34
【问题描述】:

只是想知道是否有人可以帮助我解决以下问题。

在我网站的主页上,我显示了 3 篇来自我已集成到网站中的 Wordpress 博客网站的博客文章;因此,我使索引页动态化,并添加了以下内容:

<div id="from-blog">
    <h2>Latest Blog Posts</h2>
    <ul>
          <?php while (have_posts()): the_post(); ?>
        <li class="latest-entry-front">
              <span class="post-date">
                <span class="date-text">
                  <span class="month"><?php the_time('M') ?></span><br/>
                  <span class="day"><?php the_time('d') ?></span>
                </span>
              </span>
            <a href="<?php the_permalink(); ?>" title="Read full post"><?php the_title(); ?></a></li>
              <?php endwhile; ?>
        </ul>
    </div>

这里的问题是帖子显示的是完整的博客标题,看起来很乱,这是一个很长的博客帖子标题;因此有没有办法可以修剪它以仅显示最大数量的字符,然后在它之后 [...] 或 ...

非常感谢任何帮助!

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    编辑:

    $title = the_title('', '', false);    
    if(strlen($title) > $max_len)
    {
      echo substr($title, 0, $max_length) . "...";
    }
    else
    {
      echo $title
    }
    

    应该适合你。

    http://php.net/manual/en/function.substr.php

    【讨论】:

    • 当然可以账单。在 wordpress 中,您只需将 $title 替换为 the_title('', '', false) 或创建一个变量: $title = the_title('', '', false); codex.wordpress.org/Function_Reference/the_title抱歉,由于某种原因无法在此处发布代码。
    • 好的,我只想在 Wordpress 博客之外的索引页面上修剪长度,只显示 3 个帖子;因此,我没有办法在页面上和我拥有的代码中做到这一点吗?谢谢
    • 您只需将调用 the_title() 替换为提供的代码。您可以像 Vlad 那样拨打电话,无论如何都会将标题缩减为 50,但如果您的标题超过 50,则不会添加“...”,它只会修剪标题。这有意义吗?
    • 你知道比尔,我不太确定那个。如您所见,我对此也很陌生。不过,我将不胜感激。
    猜你喜欢
    • 2012-01-19
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多