php - 将 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>
我需要这样做,因为我将在这两个部分之间添加一个滑块。
解决方案
如果您想按字数统计使用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>';
推荐阅读
- oracle - 使用 Informatica 时,源 Oracle 数据库和目标相同 Oracle 数据库中相同 sql 查询的不同时间戳
- bash - 如何合并不同的文本文件?
- macos - 在 MacOS 上,PyTorch 未启用和访问 GPU 驱动程序和 CUDA
- java - 了解 javacard 应用程序中的网络类型(2G、3G、4G)
- javascript - 为什么我在获取响应中的字符串比较无法正常工作?
- java - 正则表达式允许单个下划线和连字符不在开头或结尾
- python - 从字典中的迭代中删除“无”
- neo4j - 在 neo4j 社区中,neo4j DB 是如何解析 Cypher 语句的,无论是 CREATE、UPDATE 还是 DELETE?
- python - TypeError:不可排序的类型:int() < str()
- google-analytics - 代码仍在 Google 跟踪代码管理器中运行