WordPress按帖子ID按数组中的确切顺序获取帖子

拉奇特·古普塔(Rachit Gupta)

如何按帖子ID和数组中的顺序获取帖子?

我试过了:

<?php

$ids = array(548,555,587,583,585);

$my_query = query_posts(array('post__in' => $ids));

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);
//do something
echo '<p>TITLE: ';the_title();echo ' - ';the_ID(); '</p>';
the_content();

}

?>
史蒂文·琼斯

首先-您可能应该为此使用WP_Query(除非您有非常特定的用例)。

http://codex.wordpress.org/Class_Reference/WP_Query

要订购您的帖子,您可以使用:

array('post__in' => $ids, 'orderby' => 'post__in'))

http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

注意:仅自WP 3.5起

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章