我在$ 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并按顺序打印这些帖子?

有帮助吗?

解决方案

你可能不得不打破标准的WP Loop ......

尝试并使用 get_post()函数,该函数获取帖子的ID并返回包含通常OBJECT或Associate或Numeric Array格式的帖子详细信息的对象。

请参阅 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 ;
        }
}

希望这会有所帮助:)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top