首页 > 解决方案 > 将 the_content() 拆分为前 x 个字母的 2 个 div

问题描述

在 single.php 上,这用于显示帖子内容:

<div class="post-content">
    <?php the_content(); ?>
</div>

我怎么能破坏内容(在 WP 编辑器中不使用更多标签)所以它看起来像这样:

<div class="post-content">
    first 100 letters of the post
</div>
<div class="post-content">
    remaining part of the post
</div>

我需要这样做,因为我将在这两个部分之间添加一个滑块。

标签: phpwordpress

解决方案


如果您想按字数统计使用wp_trim_words功能拆分内容。这是您可以使用的代码示例(我没有检查它)。

<?php $content = get_the_content();?>
$first_slice = wp_trim_words($content,100);
$second_slice = substr($content,strlen($first_slice),strlen($content));
echo '<div class="post-content">'.wpautop($first_slice).'</div>';
echo '<div class="post-content">'.wpautop($second_slice).'</div>';

推荐阅读