1
我在這裏使用此代碼來使用自定義分類從媒體庫中獲取圖像。它獲得的圖像網址很好,但我想獲得圖像ID和標題。我怎麼做? 這是我到目前爲止。WordPress的媒體庫自定義分類獲取圖像
function get_images_from_media_library($cat) {
$args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => 6,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'gallery-category',
'field' => 'slug',
'terms' => $cat
)
)
);
$query_images = new WP_Query($args);
$images = array();
foreach ($query_images->posts as $image) {
$images[]= $image->guid;
echo $image->ID; // Returns image ID, but I need it in display_images_from_media_library function
}
return $images;
}
function display_images_from_media_library($cat) {
$imgs = get_images_from_media_library($cat);
foreach($imgs as $img) {
$html .= '<img src="' . $img . '" alt="">';
}
return $html;
}