I'm using the below code to get all image URL's attached to a post.

global $post;

 $thumbnail_ID = get_post_thumbnail_id();

 $images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

 if ($images) :

     foreach ($images as $attachment_id => $image) :

         $img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
         if ($img_alt == '') : $img_alt = $image->post_title; endif;

         $big_array = image_downsize( $image->ID, 'large' );
         $img_url = $big_array[0];
 endforeach; endif; }

The output I get is something like this:


What I need to find is the attachment page URL for this image which will be something like this https://www.example.com/post-name/image-22

I tried using wp_get_attachment_image, but the output wasn't what I needed.

any idea how can i do that?

1 Answers

Second2None On Best Solutions

You are looking for get_attachment_link to return a pretty link you need to make sure your permalink structure is set to pretty links.


Example from page:

$attachment_id = 1; // ID of attachment
$attachment_page = get_attachment_link( $attachment_id ); 
<a href="<?php echo $attachment_page; ?>"><?php echo get_the_title( $attachment_id ); ?></a>