Show ids of displayed media library items

Actually, wordpress stores the media library files also as a post format.

so you can follow this way to get your attachment media file and their id’s.

Hope this will help you.

function get_images_from_media_library() {
$args = array(
    'post_type' => 'attachment',
    'post_mime_type' =>'image',
    'post_status' => 'inherit',
    'posts_per_page' => 5,
    'orderby' => 'rand'
);
$query_images = new WP_Query( $args );
$images = array();
foreach ( $query_images->posts as $image) {
    $images['id']= $image->ID;
    $images['src']= $image->guid;
}
return $images;
}

Next step is to create the HTML gallery or Your shortcode:

function display_images_from_media_library() {

$imgs = get_images_from_media_library();
$html="<div id="media-gallery">";

foreach($imgs as $img) {

    $html .= '<img src="' . $img['src'] . '" alt="' . $img['id'] . '" />';

}

$html .= '</div>';

return $html;
}

You can use this code for your gallery shortcode.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)