Answers:
$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => - 1,
);
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image ) {
$images[] = wp_get_attachment_url( $image->ID );
}
现在所有图片url都在$images
;
$media_query = new WP_Query(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => -1,
)
);
$list = array();
foreach ($media_query->posts as $post) {
$list[] = wp_get_attachment_url($post->ID);
}
// do something with $list here;
在数据库中查询所有媒体库项目(不仅是帖子中附加的项目),获取它们的url,然后将它们全部转储为$list
数组。
<?php
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'medium' );
}
?>
这会拉出帖子/页面的所有附件。将更多图片附加到帖子上,它将被列出
好的,您使用此代码来显示媒体库中的所有图像!
$args = array(
'post_type' => 'attachment',
'post_status' => 'published',
'posts_per_page' =>25,
'post_parent' => 210, // Post-> ID;
'numberposts' => null,
);
$attachments = get_posts($args);
$post_count = count ($attachments);
if ($attachments) {
foreach ($attachments as $attachment) {
echo "<div class=\"post photo col3\">";
$url = get_attachment_link($attachment->ID);// extraigo la _posturl del attachmnet
$img = wp_get_attachment_url($attachment->ID);
$title = get_the_title($attachment->post_parent);//extraigo titulo
echo '<a href="'.$url.'"><img title="'.$title.'" src="'.get_bloginfo('template_url').'/timthumb.php?src='.$img.'&w=350&h=500&zc=3"></a>';
echo "</div>";
}
}
如果您知道显示分页的方法,请回答。
似乎它已经有一段时间没有更新了,但是Media Library Gallery插件可能是一个很好的例子。
这只是使用和的此答案的简短版本。get_posts()
array_map()
$image_ids = get_posts(
array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => - 1,
'fields' => 'ids',
) );
$images = array_map( "wp_get_attachment_url", $image_ids );