下面是我如何解决您的问题的(事实上,我想我以前写过这样的代码,但现在找不到):
$blogs = array( 4, 1 );
$all_posts = array();
foreach( $blogs as $blog ) {
switch_to_blog( $blog );
$args = array( \'posts_per_page\' => 10, \'orderby\' => \'date\', \'order\' => \'DESC\');
$blog_posts = get_posts( $args );
foreach( $blog_posts as $blog_post ) {
// Add the correct permalink to the Post object
$blog_post->permalink = get_the_permalink( $blog_post->ID );
$all_posts[] = $blog_post;
}
restore_current_blog();
}
usort( $all_posts, \'__sort_by_date\' );
// Now you can display all your posts
foreach( $all_posts as $post ) {
setup_postdata( $post );
the_title( \'<a href="\' . $post->permalink . \'">\', \'</a>\' );
}
function __sort_by_date( $a, $b ) {
$a_date = strtotime( $a->post_date );
$b_date = strtotime( $b->post_date );
if( $a_date === $b_date ) { return 0; }
if( $a_date > $b_date ) { return 1; }
return -1;
}
Note: 我已经更新了代码,以便在适当的站点上为每个帖子获得正确的永久链接(根据下面的评论/回答)。
strtotime()
the_title()
get_the_permalink()