wordpress 获取指定文章ID组的文章列表

有时候,我们需要显示指定文章ID数组的文章列表,那么看看下面代码,可能会帮助到你:

方法一:

<?php
$ids = array(548,555,587,583,585);
$my_query = query_posts(array('post__in' => $ids,'post_type'=> 'parks'));
global $post;
foreach ($my_query as $post) {
 $posts_by_id[$post->ID] = $post;
}
foreach ($ids as $id) {
 if (!$post = $posts_by_id[$id]) continue;
 setup_postdata($post);
 echo '<p>TITLE: ';the_title();echo ' - ';the_ID(); '</p>';
 the_content();
}
?>

方法二:(如果需要分页)

<?php
global $wp_query;
 $args = array_merge( $wp_query->query_vars, array('post__in' => $ids, 'paged' => $paged) );
 query_posts( $args );
 while ( have_posts() ) : the_post();
 //代码
 endwhile;wp_reset_query();
?>