这顿午餐我没什么事可做,所以我为此准备了一个小函数。主要是获取网络中的所有站点及其id。为此,我通过类使用一个简单的SQL查询wpdb:
$site_ids = $wpdb->get_results( "SELECT blog_id from $wpdb->blogs" );
它将返回
blog_id
. 当我有所有的网站都在运行
foreach 在
site_ids
和
switch_to_blog 像这样:
// Within the foreach
switch_to_blog( $site_id->blog_id );
在当前的博客中,我运行了一个简单的
get_posts 返回附件数。
另一个foreach
在我收到的邮件上get_posts
使用array_push 设置post_date
as键和post_id
作为值。post\\u日期用于使post在数组中唯一,然后根据时间戳对图像进行排序。
下一步是循环使用我们设置的数据post_date
作为键和post_id
作为使用函数获取图像的值wp_get_attachment_image(). 也将使用array_push
将时间戳添加为键,将图像添加为值。我们需要时间戳来对图像进行排序。(最新优先)使用krsort 在我们回显图像之前。
Here is the full code:
function wpse_77816_ms_last_images( $num_of_images, $size ) {
global $wpdb;
// Setup array
$images = array();
$site_ids = $wpdb->get_results( "SELECT blog_id from $wpdb->blogs" );
foreach ( $site_ids as $site_id ) {
// Swtch to blog
// http://codex.wordpress.org/WPMU_Functions/switch_to_blog
switch_to_blog( $site_id->blog_id );
$posts = get_posts( array(
\'numberposts\' => $num_of_images,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\'
));
// Setup array
$data = array();
foreach( $posts as $post ) {
// return array with post_id and post_dates
$data[strtotime( $post->post_date )] = $post->ID;
}
// Get the images
foreach( $data as $d => $id ) {
$images[$d] = wp_get_attachment_image( $id, $size );
}
// Restor switch_to_blog
restore_current_blog();
}
// Sort on high to low (time)
krsort( $images );
foreach( $images as $image ) {
echo $image;
}
}
一
function wpse_77816_ms_last_images( $num_of_images, $size ) {
global $wpdb;
// Setup array
$images = array();
$site_ids = $wpdb->get_results( "SELECT blog_id from $wpdb->blogs" );
foreach ( $site_ids as $site_id ) {
// Swtch to blog
// http://codex.wordpress.org/WPMU_Functions/switch_to_blog
switch_to_blog( $site_id->blog_id );
$posts = get_posts( array(
\'numberposts\' => $num_of_images,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\'
));
// Setup array
$data = array();
foreach( $posts as $post ) {
// return array with post_id and post_dates
$data[strtotime( $post->post_date )] = $post->ID;
}
// Get the images
foreach( $data as $d => $id ) {
$images[$d] = wp_get_attachment_image( $id, $size );
}
// Restor switch_to_blog
restore_current_blog();
}
// Sort on high to low (time)
krsort( $images );
foreach( $images as $image ) {
echo $image;
}
}