SSomeone可以告诉我用它的id获取帖子的最好方法是什么?
我使用的是:
$query =query_posts(‘post_id=’.$_GET‘’php_post_id‘);全局$post;
foreach ($query as $post):
做一些事情..。
这将返回一个包含所有post的数组
发布于 2012-06-22 17:46:04
get_post( $post_id, $output );所以在实践中看起来是这样的:
$my_id = 7;
$post_id_7 = get_post($my_id);有关帖子的参数和字段的更多参考,请访问:http://codex.wordpress.org/Function_Reference/get_post
更新:当你需要通过id获取单个帖子时,它是的最佳实践,不需要cicles。
发布于 2016-11-13 06:13:46
如果你想获得一个带有你已经知道的ID的帖子,或者是从其他来源得到的,我建议使用以下代码。
$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. 发布于 2018-12-18 23:45:51
将post_id=更改为p=。
$setQuery = 'p='.$_GET['php_post_id'];
query_posts($setQuery);点击此链接查看:Retrieve a Particular Post
https://stackoverflow.com/questions/11153741
复制相似问题