get_the_content()
云策文档标注
概述
get_the_content() 是 WordPress 核心函数,用于检索文章内容,返回原始字符串而不应用 the_content 过滤器。它支持可选参数控制更多链接文本、摘要剥离和指定文章。
关键要点
- 函数返回文章内容的原始字符串,不自动嵌入视频或扩展短代码,与 the_content() 不同。
- 参数包括 $more_link_text(更多链接文本,默认 null)、$strip_teaser(是否剥离摘要,默认 false)和 $post(文章对象或 ID,默认 null)。
- 在循环内使用时获取当前文章内容;循环外需通过 $post 参数指定文章。
- 函数内部处理分页、密码保护和更多标签,并调用相关函数如 generate_postdata() 和 has_block()。
- 从 WordPress 5.2.0 开始添加 $post 参数,需注意版本兼容性。
代码示例
// 基本用法:获取内容并应用过滤器以显示
$content = get_the_content( 'Read more' );
echo apply_filters( 'the_content', $content );
// 检查内容是否为空(在循环内)
$the_content = apply_filters( 'the_content', get_the_content() );
if ( !empty($the_content) ) {
echo $the_content;
}
// 使用 $post 参数指定文章
$post = get_post(12);
$content = get_the_content( null, false, $post );
echo apply_filters( 'the_content', $content );注意事项
- get_the_content() 不应用 the_content 过滤器,如需完整显示内容(如嵌入视频),需手动调用 apply_filters( 'the_content', get_the_content() )。
- 在循环外使用时,确保传递正确的 $post 参数,否则可能返回空字符串。
- 注意 WordPress 版本,$post 参数仅在 5.2.0 及以上版本可用。
- 如果遇到 generate_postdata() 问题,确保全局 $wp_query 对象已设置。
原文内容
Retrieves the post content.
Parameters
Source
function get_the_content( $more_link_text = null, $strip_teaser = false, $post = null ) {
global $page, $more, $preview, $pages, $multipage;
$_post = get_post( $post );
if ( ! ( $_post instanceof WP_Post ) ) {
return '';
}
/*
* Use the globals if the $post parameter was not specified,
* but only after they have been set up in setup_postdata().
*/
if ( null === $post && did_action( 'the_post' ) ) {
$elements = compact( 'page', 'more', 'preview', 'pages', 'multipage' );
} else {
$elements = generate_postdata( $_post );
}
if ( null === $more_link_text ) {
$more_link_text = sprintf(
'<span aria-label="%1$s">%2$s</span>',
sprintf(
/* translators: %s: Post title. */
__( 'Continue reading %s' ),
the_title_attribute(
array(
'echo' => false,
'post' => $_post,
)
)
),
__( '(more…)' )
);
}
$output = '';
$has_teaser = false;
// If post password required and it doesn't match the cookie.
if ( post_password_required( $_post ) ) {
return get_the_password_form( $_post );
}
// If the requested page doesn't exist.
if ( $elements['page'] > count( $elements['pages'] ) ) {
// Give them the highest numbered page that DOES exist.
$elements['page'] = count( $elements['pages'] );
}
$page_no = $elements['page'];
$content = $elements['pages'][ $page_no - 1 ];
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
if ( has_block( 'more', $content ) ) {
// Remove the core/more block delimiters. They will be left over after $content is split up.
$content = preg_replace( '/<!-- /?wp:more(.*?) -->/', '', $content );
}
$content = explode( $matches[0], $content, 2 );
if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) {
$more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
}
$has_teaser = true;
} else {
$content = array( $content );
}
if ( str_contains( $_post->post_content, '<!--noteaser-->' )
&& ( ! $elements['multipage'] || 1 === $elements['page'] )
) {
$strip_teaser = true;
}
$teaser = $content[0];
if ( $elements['more'] && $strip_teaser && $has_teaser ) {
$teaser = '';
}
$output .= $teaser;
if ( count( $content ) > 1 ) {
if ( $elements['more'] ) {
$output .= '<span id="more-' . $_post->ID . '"></span>' . $content[1];
} else {
if ( ! empty( $more_link_text ) ) {
/**
* Filters the Read More link text.
*
* @since 2.8.0
*
* @param string $more_link_element Read More link element.
* @param string $more_link_text Read More text.
*/
$output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink( $_post ) . "#more-{$_post->ID}" class="more-link">$more_link_text</a>", $more_link_text );
}
$output = force_balance_tags( $output );
}
}
return $output;
}
Hooks
- apply_filters( ‘the_content_more_link’, string $more_link_element, string $more_link_text )
-
Filters the Read More link text.
Skip to note 7 content
Hay
Note that
get_the_contentdoesn’t return the same thing as whatthe_contentdisplays. For that you need to do this:$content = apply_filters( 'the_content', get_the_content() ); echo $content;Skip to note 8 content
Codex
Basic Usage
Display the post content, ending with “Read more” if needed
$content = get_the_content( 'Read more' ); echo apply_filters( 'the_content', $content );Skip to note 9 content
netzgestaltung
find out if the_content has content before output
in functions.php and similar files:
// write inside the loop $the_content = apply_filters('the_content', get_the_content()); if ( !empty($the_content) ) { echo $the_content; } // with post object by id $post = get_post(12); // specific post $the_content = apply_filters('the_content', $post->post_content); if ( !empty($the_content) ) { echo $the_content; }as function
// call inside the loop or with ID // taken from has_excerpt() // <a href="https://developer.wordpress.org/reference/functions/has_excerpt" rel="ugc">https://developer.wordpress.org/reference/functions/has_excerpt</a> function mytheme_has_content( $post = 0 ){ $post = get_post( $post ); return ( !empty(apply_filters('the_content', $post->post_content)) ); }template inside the loop:
have_posts() ) {?> have_posts() ) { $customQuery->the_post(); ?> <!-- html --> <div class="content"> </div>Skip to note 10 content
tylerlwsmith
If you’re having trouble getting the $post parameter to work, make sure your WordPress is up-to-date: $post was only added in WordPress 5.2.
Skip to note 11 content
Stéphane Le Roy
If you use
get_the_content()before the global $wp_query object is set, the postmeta are not well generated becausegenerate_postdata()use $wp_query.To get around this issue, just construct a global $wp_query object before calling
get_the_content()global $wp_query; $wp_query = new WP_Query();Skip to note 12 content
Lovro Hrust
If you need to embed content coded as URL in post content, like youtube video, use following:
global $wp_embed; $content = $wp_embed->autoembed( $content );This is the way how it is handled in the WordPress core.