使用get_the_content()在WordPress中检索帖子内容
WordPress get_the_content
函数会在 WordPress 循环中检索当前文章的内容。它可用于在自定义模板上显示文章的主要内容,或在显示之前对其进行操作。
通过使用get_the_content
,开发人员可以轻松访问帖子的主要内容并以各种方式使用它,例如以自定义布局显示它、应用自定义样式或在将其输出给用户之前执行其他处理。
推荐:[最新版]WordPress SEO插件Rank Math Pro
get_the_content()基本语法
描述
WordPress PHP get_the_content() 函数检索帖子内容。
用法
$content = get_the_content('Read more');
echo apply_filters('the_content', $content);
- $more_link_text (string) 可选:当有更多文本时的内容。默认值:null
- $strip_teaser (bool) 可选:在更多文本之前删除预告内容。默认值:false
- $post (WP_Post|object|int) 可选:WP_Post 实例或帖子 ID/对象。默认值:null
get_the_content() 返回的内容与 the_content 所显示的内容不同
get_the_content()函数
get_the_content()
是 WordPress 中用于获取文章或页面主要内容的函数。与 the_content()
不同,get_the_content()
并不会直接输出内容到页面上,而是返回内容的原始文本,这使得它非常适合在需要对内容进行进一步处理时使用(源文件可参考这里)
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;
}
推荐:使用wp_set_post_tags()为WordPress帖子设置标签
如何使用get_the_content()
如何使用 get_the_content 函数获取特定帖子的内容,此代码片段使用函数检索 ID 为 123 的特定帖子的内容get_the_content
,然后在段落标签中回显它。
$post_id = 123; // Replace with the ID of the post you want to retrieve the content from
$content = get_the_content(null, false, $post_id);
echo '<p>' . $content . '</p>';
如何使用 get_the_content 函数获取当前帖子的内容,此代码片段使用函数检索当前帖子的内容get_the_content
,然后在段落标签中回显它。
$content = get_the_content();
echo '<p>' . $content . '</p>';
如何使用 get_the_content 函数获取特定帖子的内容而不进行格式化,此代码片段检索 ID 为 123 的特定帖子的内容,不使用get_the_content
函数进行格式化,然后在段落标签中回显它。
$post_id = 123; // Replace with the ID of the post you want to retrieve the content from
$content = get_the_content('', false, $post_id);
echo '<p>' . $content . '</p>';
输出前检查 the_content 是否有内容,在 functions.php 和类似文件中输出之前,检查 the_content 是否有内容:
// 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;
}
使用自定义函数检查 the_content 是否有内容,在循环内或使用帖子 ID 调用自定义函数:
function mytheme_has_content($post = 0) {
$post = get_post($post);
return (!empty(apply_filters('the_content', $post->post_content)));
}
// 循环内的模板
if ($customQuery->have_posts()) {
while ($customQuery->have_posts()) {
$customQuery->the_post();
$the_content = apply_filters('the_content', get_the_content());
if (!empty($the_content)) {
echo '<div class="content">' . $the_content . '</div>';
}
}
wp_reset_postdata();
}
结论
get_the_content
函数是 WordPress 中检索帖子或页面内容的有效功能。通过此功能,开发人员可以轻松访问和操作其主题或插件中的内容。无论是用于以自定义布局显示内容、执行自定义修改还是任何其他目的,它get_the_content
都提供了一种灵活便捷的方式来处理帖子内容
推荐:[最新版]WP Speed of Light Pro插件WordPress速度优化插件