首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用帖子id查询帖子

使用帖子id查询帖子
EN

Stack Overflow用户
提问于 2012-06-22 17:39:22
回答 5查看 46K关注 0票数 2

SSomeone可以告诉我用它的id获取帖子的最好方法是什么?

我使用的是:

$query =query_posts(‘post_id=’.$_GET‘’php_post_id‘);全局$post;

foreach ($query as $post):

做一些事情..。

这将返回一个包含所有post的数组

EN

回答 5

Stack Overflow用户

发布于 2012-06-22 17:46:04

代码语言:javascript
复制
get_post( $post_id, $output );

所以在实践中看起来是这样的:

代码语言:javascript
复制
$my_id = 7;
$post_id_7 = get_post($my_id);

有关帖子的参数和字段的更多参考,请访问:http://codex.wordpress.org/Function_Reference/get_post

更新:当你需要通过id获取单个帖子时,它是的最佳实践,不需要cicles。

票数 3
EN

Stack Overflow用户

发布于 2016-11-13 06:13:46

如果你想获得一个带有你已经知道的ID的帖子,或者是从其他来源得到的,我建议使用以下代码。

代码语言:javascript
复制
$args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'p' => $id,   // id of the post you want to query
    );
    $my_posts = new WP_Query($args);  

   if($my_posts->have_posts()) : 

        while ( $my_posts->have_posts() ) : $my_posts->the_post(); 

          get_template_part( 'template-parts/content', 'post' ); //Your Post Content comes here

        endwhile; //end the while loop

endif; // end of the loop. 
票数 2
EN

Stack Overflow用户

发布于 2018-12-18 23:45:51

post_id=更改为p=

代码语言:javascript
复制
$setQuery = 'p='.$_GET['php_post_id'];
query_posts($setQuery);

点击此链接查看:Retrieve a Particular Post

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11153741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档