使用自定义分类法显示媒体库图像

I have added a custom taxonomy 'Elements' to my Wordpress Media Library so I can tag images and sort them by tag.

I now want to output all the images automatically in an taxonomy-elements.php archive template (or archive-elements.php?).

So for example all images with the Elements tag 'light' would appear at /elements/light.

I also want to query all Elements tags to display a list of links anywhere on the site that would link to these archive pages. this list would also display the amount of images for each tag.

e.g:

Elements: Light(5) Space(2) Red(4) Blue(8)

My registered custom taxonomy:

// register new taxonomy which applies to attachments
function wptp_add_elements_taxonomy() {
    $labels = array(
        'name'              => 'Elements',
        'singular_name'     => 'Element',
        'search_items'      => 'Search Elements',
        'all_items'         => 'All Elements',
        'parent_item'       => 'Parent Element',
        'parent_item_colon' => 'Parent Element:',
        'edit_item'         => 'Edit Element',
        'update_item'       => 'Update Element',
        'add_new_item'      => 'Add New Element',
        'new_item_name'     => 'New Element Name',
        'menu_name'         => 'Elements',
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'query_var' => 'true',
        'rewrite' => 'true',
        'show_admin_column' => 'true',
    );

    register_taxonomy( 'elements', 'attachment', $args );
}
add_action( 'init', 'wptp_add_elements_taxonomy' );

Trying to output images from the Elements custom taxonomy:

function get_images_from_media_library($elements) {
    $args = array(
        'post_type' => 'attachment',
        'post_mime_type' =>'image',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
        'orderby' => 'rand',
        'tax_query' => array(
            array(
            'taxonomy' => 'elements',
            'field' => 'slug',
            'terms' => $elements
            )
          )
        );
  $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($elements) {
  $imgs = get_images_from_media_library($elements);
  foreach($imgs as $img) {
     $html .= '<img src="' . $img . '" alt="">';
  }
  return $html;
}