在媒体库中获取所有图像?
-
-
您是说整个媒体库中(即站点范围内)的所有图片吗?Do you mean all images in the entire Media library (i.e., site-wide)?
- 0
- 2011-03-12
- ZaMoose
-
6 个回答
- 投票数
-
- 2011-03-12
$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 ); }
所有图片的网址现在都在
$images
;$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 ); }
All the images url are now in
$images
;-
嗯..看起来@somatic击败了我.与上面的解决方案不同,我的只能获取图像.um.. looks like @somatic has beat me to it. Unlike his solution above, mine will only get images.
- 0
- 2011-03-12
- Azizur Rahman
-
显然,我们的方法是相似的...并且azizur是正确的,将'post_mime_type'添加到任一查询中将缩小返回的类型. 要考虑的一件事:Guid通常确实包含图像的完整URL,但这不是可靠的来源.它是静态的,仅在创建帖子时生成一次,并且是基于当前站点url和媒体文件夹结构构建的.但是该文件夹结构和域可能会在某个时候发生变化,然后guid不再是实际的图像URL,而只是它创建时的记录...obviously our methods are similar... and azizur is right, adding the 'post_mime_type' to either query will narrow the types returned. one thing to consider: the guid often does contain the full url to the image, but it is not a reliable source. It is static, generated only once when the post is created, and is built on the current site url and the media folder structure. But that folder structure *and* the domain could change at some point, and then the guid is not the actual image URL anymore, just a record of what it was when it was created...
- 2
- 2011-03-13
- somatic
-
这个答案是"错误的".它不会从媒体库获取图像.它获取帖子内部使用的图像.找不到未使用的图像!This answer is **WRONG**. It does not get images from Media Library. It gets images used inside posts. Unused images are not found!
- 1
- 2011-10-10
- Christian
-
@Christian-错了吗?还是我应该问"仍然"错误?我意识到我将在大约2年后发表评论,但是我在WP 3.6上进行了尝试,并且收到的图像是我刚刚添加到媒体库中的,而没有将它们添加到任何帖子中:/@Christian - is it wrong? Or should I ask 'still' wrong? I realise I'm commenting almost 2 years later, but I tried this out on WP 3.6 and I'm receiving images that I've just added to the media library without adding them to any posts :/
- 0
- 2013-08-16
- Chris Kempen
-
可能是一个愚蠢的问题,但是我现在将如何获得不同的图像尺寸?Might be a stupid question, but how would I now get the different image sizes?
- 0
- 2016-07-17
- Frederik Witte
-
@FrederikWitte,您可以将[get_intermediate_image_sizes](https://developer.wordpress.org/reference/functions/get_intermediate_image_sizes/)和[wp_get_attachment_image_src](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/)结合使用所有网址.@FrederikWitte you can combine [get_intermediate_image_sizes](https://developer.wordpress.org/reference/functions/get_intermediate_image_sizes/) and [wp_get_attachment_image_src](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/) to get all the urls.
- 0
- 2016-07-17
- Azizur Rahman
-
yaaaaaaaaaaaaaaaaaaasyaaaaaaaaaaaaaaaaas
- 0
- 2018-02-22
- Zach Smith
-
- 2011-03-12
$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
数组中.$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;
Query the db for all media library items (not just ones attached to posts), grab their url, dump them all in
$list
array. -
- 2011-03-12
<?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' ); } ?>
这将拉出帖子/页面的所有附件. 在帖子中附加更多图片,它将被列出
<?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' ); } ?>
This pulls all attachments for a post/page. Attach more images to a post, and it will be listed
-
- 2012-02-27
好,您使用此代码显示媒体库中的所有图像!
$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>"; } }
如果您知道显示分页的方法,请回答.
ok y used this code for show ALL images in media Library!
$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>"; } }
and if you know method for show pagination, please answer.
-
- 2011-03-12
好像已经有一段时间没有更新了,但是 Media图书馆图库插件可能是一个很好的例子.
It looks as though it hasn't been updated in a while, but the Media Library Gallery plugin might be a good example to start looking at.
-
- 2016-01-20
这只是该 answer 的较短版本,使用的是
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 );
This is just a shorter version of this answer using
get_posts()
andarray_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 );
是否可以在媒体库中获取 ALL 图片的URL?
我认为这是网站拥有"图片"页面的简单方法,该页面仅从媒体库中提取所有图像,因为只有在某些情况下才有必要.
我不需要有关如何创建"图片"页面的说明,只需了解如何提取所有图像URL.谢谢!