SSomeone可以告诉我什么是使用它的ID获得帖子的最佳方式?查询帖子使用帖子ID
我'使用该:
$查询= query_posts( 'POST_ID =' $ _ GET [ 'php_post_id']); global $ post;
的foreach($查询为$后):
做的东西......
这是返回所有的数组后
SSomeone可以告诉我什么是使用它的ID获得帖子的最佳方式?查询帖子使用帖子ID
我'使用该:
$查询= query_posts( 'POST_ID =' $ _ GET [ 'php_post_id']); global $ post;
的foreach($查询为$后):
做的东西......
这是返回所有的数组后
下面是代码使用query_post如果你知道如何获取后ID。
<?php
$my_query = query_posts('post_id=111&post_type=parks'); // in place of 111 you need to give desired ID.
global $post;
foreach ($my_query as $post) {
setup_postdata($post);
the_title();
the_content();
}
?>
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。
如果你正在寻找一个你已经知道或从其他来源获得的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.
我一直在使用这一点,但不知道为什么返回所有职位的数组...我有固定与此:\t'post', \t'post__in'=> array($ _ GET ['php_post_id']), \t); $ post_query = new WP_Query($ args); while($ post_query-> have_posts()):$ post_query-> the_post(); endforeach; –
好吧然后。我希望你的问题现在得到解决? – Subhajit
使用“p”参数而不是“post_in”来获得一个必需的帖子! – dk1