substring - 如果文本被剪切,则在末尾添加 ...

substring - add ... in the end if text is cut

我在 wordpress 网站上有一个代码 (front-page.php),如果文本超过 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>

您可以调整您的代码并仅在需要时放置...:

<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>
<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>