[英]How can I have wordpress print posts whose IDs appear in an array?
I have an array of post IDs contained in $postarray. 我有一个数组包含在$ postarray中的帖子ID。 I would like to print the posts corresponding to these IDs in Wordpress. 我想在Wordpress中打印与这些ID对应的帖子。 The code I am using is as follows: 我使用的代码如下:
query_posts(array('post__in' => $postarray));
if (have_posts()) :
while (have_posts()) : the_post();
the_title();
the_excerpt();
endwhile;
endif;
Despite this, the loop prints the most recent posts and not the posts contained in the array. 尽管如此,循环仍会打印最新的帖子,而不是数组中包含的帖子。 How can I have wordpress utilize the post IDs I supply in the array and print those posts in order? 我如何让wordpress利用我在数组中提供的帖子ID并按顺序打印这些帖子?
You may have to break out of the standard WP Loop for this... 您可能需要为此打破标准的WP循环...
Try and use the get_post() function which takes the ID of a post and returns an object containing a the details of the post in the usual OBJECT or Associate or Numeric Array format. 尝试使用get_post()函数,该函数获取帖子的ID,并以通常的OBJECT或Associate或Numeric Array格式返回包含该帖子详细信息的对象。
See full-explanation of get_post() . 请参见get_post()的完整说明 。
You can come up with a custom routine to parse each item in the array. 您可以提出一个自定义例程来解析数组中的每个项目。 Here's a brief example: 这是一个简单的示例:
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 ;
}
}
Hope this helps :) 希望这可以帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.