通过帖子ID获取WordPress帖子内容

时间:2011-02-17 作者:m3tsys

如何通过帖子id获取WordPress帖子内容?

5 个回复
最合适的回答,由SO网友:Bainternet 整理而成

Simple as it gets

$my_postid = 12;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]>\', $content);
echo $content;
SO网友:realmag777
echo get_post_field(\'post_content\', $post_id);
SO网友:Ranuka

通过帖子id获取WordPress帖子内容的另一种方法是:

$content = apply_filters(\'the_content\', get_post_field(\'post_content\', $my_postid));
为了完成这个答案,我还添加了方法01和方法02。

方法01(计入bainternet):

$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]>\', $content);
方法02(计入realmag777):

$content = get_post_field(\'post_content\', $my_postid);
方法03:

$content = apply_filters(\'the_content\', get_post_field(\'post_content\', $my_postid));
阅读What is the best / efficient way to get WordPress content by post id and why?
SO网友:Jashwant

从wordpress 5.2.0开始,

我们现在可以使用post对象或post idget_the_content 作用

我们还可以传递$more\\u link\\u text和$strip\\u摘要

get_the_content( string $more_link_text = null, bool $strip_teaser = false, WP_Post|object|int $post = null )
我们可以一起使用apply_filters 如下所示,以获取的所有筛选器the_content

apply_filters( \'the_content\', get_the_content(null, false, $post_id)),

Reference

SO网友:DigitalDesignDj

如果您需要多个帖子,请使用get_posts(). 它只保留主查询,并返回易于循环的帖子数组。

结束