通过帖子ID
4 个回答
- 投票数
-
- 2011-02-17
简单易用
$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;
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;
-
特定字段的缩写:`$ content=get_post_field('post_content',$my_postid);`Shorthand for specific field: `$content = get_post_field('post_content', $my_postid);`
- 89
- 2011-02-17
- Rarst
-
@Bainternet我在这里很好奇...`$ content=str_replace(']]>',']]>',$ content);是什么部分?那里的目的是什么?@Bainternet I'm just curious here... what is the part `$content = str_replace(']]>', ']]>', $content);` do? what's the purpose of it there?
- 5
- 2013-11-04
- Average Joe
-
@AverageJoe其基本搜索和替换.使用the_content()时,将过滤内容.由于在上面的示例中直接检索了内容,因此作者使用搜索和替换来确保内容安全.@AverageJoe its basic search and replace. When using the_content() the content is filtered. Since in the above example the content was directly retrieved, the author has used the search and replace to make it safe.
- 2
- 2014-03-18
- Harish Chouhan
-
也许您还需要像$ content=do_shortcode(get_post_field('post_content',$my_postid));这样的do_shortcode();maybe you also need do_shortcode() like `$content = do_shortcode(get_post_field('post_content', $my_postid));`
- 3
- 2018-03-09
- cyptus
-
无论如何,有保留"more_link"的内容吗?Is there anyway to preserve the "more_link"?
- 0
- 2018-07-05
- user2128576
-
- 2012-10-05
echo get_post_field('post_content', $post_id);
echo get_post_field('post_content', $post_id);
-
最好像`echo apply_filters('the_content',get_post_field('post_content',$post_id));`这样来做.例如,当使用qTranslate时,您的解决方案是不够的.better to do it like `echo apply_filters('the_content', get_post_field('post_content', $post_id));`. For example when using qTranslate, your solution would not be enough.
- 64
- 2013-01-17
- Karel Attl
-
如果范围是获取帖子内容,就像在WordPress编辑页面中那样,这是最佳答案.This is the best answer if the scope is to get the post content as it is in the WordPress edit page.
- 5
- 2014-08-08
- mcont
-
如果没有@KarelAttl的代码,则缺少行中断.使用apply_filters代码,它可以完美地工作.Without the code from @KarelAttl line breaks where missing. With the apply_filters code it worked perfectly.
- 1
- 2015-09-23
- Alexander Taubenkorb
-
" apply_filters"是一个不错的选择,但不适用于我当前的目的.两种选择都很好.`apply_filters` is a good option, but wasn't right for my current purpose. It's good to have both options.
- 2
- 2015-11-05
- KnightHawk
-
- 2016-12-02
通过帖子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));
阅读 通过帖子ID获取WordPress内容的最佳/有效方式是什么?为什么? 问题可让您从以上三个方面了解应该使用哪个.
Another way to get a WordPress post content by post id is:
$content = apply_filters('the_content', get_post_field('post_content', $my_postid));
To complete this answer I have also added method 01 and method 02 to this answer.
Method 01 (credit goes to bainternet):
$content_post = get_post($my_postid); $content = $content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content);
Method 02 (credit goes to realmag777):
$content = get_post_field('post_content', $my_postid);
Method 03:
$content = apply_filters('the_content', get_post_field('post_content', $my_postid));
Read the What is the best / efficient way to get WordPress content by post id and why? question to get an idea about which one you should use from the above three.
-
- 2015-11-20
如果您需要多个帖子,请使用
get_posts()
一个>.它将主查询保留下来,并返回易于循环的帖子数组.If you need more than one post, use
get_posts()
. It leaves the main query alone and returns an array of posts that's easy to loop over.
如何通过帖子ID获取WordPress帖子内容?