我在$ postarray中包含了一系列帖子ID。我想在Wordpress中打印与这些ID相对应的帖子。 我使用的代码如下:
query_posts(array('post__in' => $postarray));
if (have_posts()) :
while (have_posts()) : the_post();
the_title();
the_excerpt();
endwhile;
endif;
尽管如此,循环打印最近的帖子而不是数组中包含的帖子。如何让wordpress利用我在数组中提供的帖子ID并按顺序打印这些帖子?
答案 0 :(得分:0)
你可能不得不打破标准的WP Loop ......
尝试并使用 get_post()函数,该函数获取帖子的ID并返回包含通常OBJECT或Associate或Numeric Array格式的帖子详细信息的对象。
请参阅 full-explanation of get_post() 。
您可以提出自定义例程来解析数组中的每个项目。这是一个简短的例子:
function get_posts_by_ids( $postarray = null ) {
if( is_array( $postarray ) )
foreach( $postarray as $post ) {
$post_details = get_post( $post[0] );
// Title
echo $post_details->post_title;
//Body
echo $post_details->post_content ;
}
}
希望这会有所帮助:)