【问题标题】:substring - add ... in the end if text is cut子字符串 - 如果文本被剪切,则在最后添加 ...
【发布时间】:2017-12-14 14:16:46
【问题描述】:

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

【问题讨论】:

  • 如果原始长度大于 35,则将变量设置为 true。仅当该变量为 true 时才有条件地显示点

标签: php html wordpress substr strpos


【解决方案1】:
<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>

【讨论】:

  • 我检查它。 sandbox.onlinephpfunctions.com/code/…你有什么错误?我编辑了这个答案,也许你用的是有bug的旧版本
  • @CristianRossi 我很乐意提供帮助。如果我的回答不错,请采纳。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多