the_content()
云策文档标注
概述
the_content() 是 WordPress 核心函数,用于显示文章内容,支持通过 快速标签控制摘要显示,并可通过参数自定义“阅读更多”链接文本和是否移除摘要前导内容。
关键要点
- 函数原型:the_content( $more_link_text = null, $strip_teaser = false ),其中 $more_link_text 为“阅读更多”链接文本(默认 null),$strip_teaser 控制是否移除摘要前导内容(默认 false)。
- 使用 快速标签时,在非单篇文章页面(如存档页)仅显示标签前内容,并自动添加“继续阅读”链接;在单篇文章页面(如 single.php)则忽略该标签显示全文。
- 注意事项: 标签前不能有空格(例如 无效),否则无法正常工作。
- 可通过全局变量 $more 覆盖默认行为:设置 $more = 0 仅显示 前内容,$more = 1 显示全文。
- 应用过滤器:the_content 过滤器允许通过 apply_filters( 'the_content', $content ) 修改输出内容。
代码示例
// 示例1:覆盖存档页/单页行为,仅显示<!--more-->前内容
global $more;
$more = 0;
the_content( 'More ...' );
// 示例2:在置顶文章中忽略<!--more-->标签显示全文
global $more;
if ( is_sticky() ) {
$more = 1;
the_content();
} else {
$more = 0;
the_content( __( 'Read the rest of this entry »', 'textdomain' ) );
}注意事项
- the_content() 内部调用 get_the_content() 获取内容,并通过过滤器 the_content 处理输出。
- 相关函数:get_the_content() 用于检索内容,apply_filters() 用于调用过滤器钩子。
- 版本历史:自 WordPress 0.71 引入。
原文内容
Displays the post content.
Parameters
$more_link_textstringoptional-
Content for when there is more text.
Default:
null $strip_teaserbooloptional-
Strip teaser content before the more text.
Default:
false
Source
function the_content( $more_link_text = null, $strip_teaser = false ) {
$content = get_the_content( $more_link_text, $strip_teaser );
/**
* Filters the post content.
*
* @since 0.71
*
* @param string $content Content of the current post.
*/
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
echo $content;
}
Hooks
- apply_filters( ‘the_content’, string $content )
-
Filters the post content.
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
Skip to note 6 content
Codex
Overriding Archive/Single Page Behavior
If
<a href="https://developer.wordpress.org/reference/functions/the_content/" rel="nofollow">the_content()</a>isn’t working as you desire (displaying the entire story when you only want the content above the<!--more-->Quicktag, for example) you can override the behavior with global $more.// Declare global $more (before the loop). global $more; // Set (inside the loop) to display content above the more tag. $more = 0; the_content( 'More ...' ); ?>If you need to display all of the content:
// Declare global $more (before the loop). global $more; // Set (inside the loop) to display all content, including text below more. $more = 1; the_content();Skip to note 7 content
Codex
Ignore the “More” on a Sticky Post
This will ignore the more tag in a sticky post–meaning it will display the full content even if there is a
<!--more-->in the content, but for all other posts it will display a more link.// Declare global $more (before the loop). global $more; if ( is_sticky() ) { // Set (inside the loop) to display all content, including text below more. $more = 1; the_content(); } else { $more = 0; the_content( __( 'Read the rest of this entry »', 'textdomain' ) ); }Skip to note 8 content
Codex
Include Title in “More”
Show “Continue reading ACTUAL POST TITLE” when the
<!--more-->Quicktag is used.Skip to note 9 content
nicoeg
If the content you want to display does not belong to the post declared globally you can use get_extended() which accepts the content as first parameter.
<br />
get_extended($content)<br />
Reference:
https://codex.wordpress.org/Function_Reference/get_extended
Skip to note 10 content
Codex
Designating the “More” Text
Displays the content of the post and uses “Read more …” for the more link text when the
<!--more-->Quicktag is used.