問題描述
有沒有辦法獲取媒體庫中所有影像的 URL?
我認為,這將是一個簡單的方法,一個網站有一個圖片頁面,只是從媒體畫廊拉,所有的影像,只允許在某些情況下,必要的。
我不需要如何建立圖片頁面的說明,只是如何拉取所有的圖片網址。謝謝!
最佳解決方案
$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 外掛可能是一個很好的例子,開始看。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。