>本文演示了如何使用内置<more></more>
标签将WordPress内容分为多个部分,并提供了用户友好的HTML或快捷代码的替代方案。 该方法避免了需要从内容作者那里进行编码的知识。
wordpress通常使用the_content()
输出页面/发布内容。但是,将内容分为块(例如,用于多列布局)需要采取不同的方法。 get_the_content()
提供内容作为PHP变量,但识别分区点需要仔细考虑。 通用解决方案,例如在HTML标签上分组(例如<h2></h2>
标题)或使用短代码,当前限制:HTML标签方法需要内容作者HTML知识和限制灵活性,而折叠代码则负担为编辑器以记住特定代码。
<more></more>
在主题文件夹中实现,修改或创建文件。 添加以下PHP函数:
functions.php
接下来,在主题循环中找到wp-content/themes
>呼叫(可能在
// split content at the more tag and return an array function split_content() { global $more; $more = true; $content = preg_split('//i', get_the_content('more')); for($c = 0, $csize = count($content); $c < $csize; $c++) { $content[$c] = apply_filters('the_content', $content[$c]); } return $content; }
或the_content()
)。 评论single.php
,然后用调用page.php
替换。此函数返回一个数组,其中每个元素代表由index.php
archive.php
search.php
请记住将其适应您主题的特定结构。 这种方法提供了一种干净,用户友好的方法,用于在没有复杂的编码或插件依赖项的情况下对WordPress内容进行分配。the_content()
>
split_content()
<more>
经常询问有关将WordPress内容分为多个部分的问题(常见问题解答)
<?php //Example usage: (Adapt to your theme's structure) add_filter( 'the_content', 'my_custom_content' ); function my_custom_content( $content ) { return str_replace( 'Read the rest of this page »', '', $content ); } // split content into array $content = split_content(); // output first content section in column1 echo '<div>', array_shift($content), '</div>'; // output remaining content sections in column2 echo '<div>', implode($content), '</div>'; ?>
以上是如何将WordPress内容分为两个或多列的详细内容。更多信息请关注PHP中文网其他相关文章!