substring - 如果文本被剪切,最后添加....

时间:2017-12-14 14:16:46

标签: php html wordpress substr strpos

我在wordpress网站上有一个代码(front-page.php),它剪切了文字并添加了#34; ..."如果文字超过35个字符,则为标题。 剪切是可以的,但点总是可见的。是否可以仅在文本被剪切时添加? 谢谢!

<div class="bxpd">   
    <h2><span><?= substr($post_title,0,35); ?></span>
    <a href="<?= get_post_permalink($post_id); ?>">
        <?= substr($postData->post_title, 0, 35)."..."; ?>              
    </a>
    </h2>
    <p><?= substr($postData->post_content,0,strpos($postData->post_content, ' ', 150))."..."; ?> </p>
</div>

2 个答案:

答案 0 :(得分:0)

只有在需要时才能调整代码并放置......

<div class="bxpd">   
                <h2><span><?= substr($post_title,0,35); ?></span>
                <a href="<?= get_post_permalink($post_id); ?>">
                <?= substr($postData->post_title, 0, 35)."..."; ?>

                </a>
                </h2>
                <p><?= substr($postData->post_content,0,strpos($postData->post_content, ' ', 150)).
(strlen($postData->post_content)>150?"...":""); 

?> </p>
           </div>

答案 1 :(得分:0)

<div class="bxpd">   
<h2><span><?= substr($post_title,0,35); ?></span>
<a href="<?= get_post_permalink($post_id); ?>">
    <?=
        substr($postData->post_title, 0, 35). (strlen($postData->post_title) > 35 ? "..." : ""); 
    ?>              
</a>
</h2>
<p><?= substr($postData->post_content,0,strpos($postData->post_content, ' ', 150))."..."; ?> </p>