get_attached_media works great to get attachments that have been uploaded or 'attached' to a given post or page. The problem is when you remove one of these attachments say an image from your gallery, get_attached_media still returns that image in the array because it was originally uploaded to the post/page. The only way to remove it would be to delete it permanently from your media. Vise versa, get_attached_media will return null if you create a new gallery on a page with images already existing in your media.
I am not looking for a plugin but rather a programmatic solutions to get attachments. I am using get_attached_media because it returns the original url rather than the thumbnail.
current code:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $attachments = get_attached_media( 'image', $post->ID ); ?>
<?php if ( $attachments ) : ?>
<ul>
<?php foreach ( $attachments as $attachment ) : ?>
<li><img src="<?php echo wp_get_attachment_url($attachment->ID ); ?>"/></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Thanks!