wordpress根据文章ID组来获取相关文章列表

首先指定文章ID数组,然后再从中获取相关文章列表是一种优化查询方式。

方法一:(不需要分页)

$ids = array(1,8,7);
$my_query = query_posts(array('post__in' => $ids,'post_type'=> 'posts'));
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 '

TITLE: ';the_title();echo ' - ';the_ID(); '

';
the_content();
}
?>
方法二:(如果需要分页)

--------------------------
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();
?> 这种方法可以用来优化rand()随机文章的查询功能。

#现在前往

精选留言

文章,wordpress,根据,ID
sample
2021-05-18
写留言
签到
投稿
QQ咨询
返回顶部