Title: Function: the_attachment_link ()
Last modified: August 20, 2016

---

# Function: the_attachment_link ()

 *  Resolved [MDavid-ITA](https://wordpress.org/support/users/mdavid-ita/)
 * (@mdavid-ita)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/function-the_attachment_link/)
 * I’m working on a WP template and I would like make a feature in which the first
   image of a post is shown and the excerpt of the post is overlapped to the image
   on a opaque white background.
 * I know there are a lot of Plugins doing that, but I’d prefer to build my own 
   template bottom up in order to deeply understand WP poetry 😉
 * Unfortunately I am unable to retrieve attached image!
 * The first attempt was to use **the_attachment_link()** function. Following is
   the code:
 *     ```
       <?php query_posts('posts_per_page=1'); ?>
           <?php if ( have_posts() ) : the_post(); ?>
           <h1><?php the_title(); ?></h1>
           <?php the_attachment_link(1); ?>
           <?php the_excerpt(); ?>
           <?php endif; ?>
       ```
   
 * Probably I did not really understand the differences between attached images 
   and/or inserted images.
 * Can someone help?
    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/function-the_attachment_link/#post-2263791)
 * You might be better off using [get_posts()](http://codex.wordpress.org/Function_Reference/get_posts)
   to grab the children (attachments).
 *  Thread Starter [MDavid-ITA](https://wordpress.org/support/users/mdavid-ita/)
 * (@mdavid-ita)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/function-the_attachment_link/#post-2263795)
 * I changed:
 * `<?php the_attachment_link(1); ?>`
 * to:
 *     ```
       <?php
       $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => get_the_ID() );
       $attachments = get_posts($args);
       if ($attachments) {
       	foreach ( $attachments as $attachment ) {
       		the_attachment_link( $attachment->ID , true );
       	}
       }
       ?>
       ```
   
 * The images is displayed properly but, unfortunately, I only can choose between
   original size image or thumbnail. I need to show the image the same size has 
   been attached. :-/ I thought it was simpler.
 *  Thread Starter [MDavid-ITA](https://wordpress.org/support/users/mdavid-ita/)
 * (@mdavid-ita)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/function-the_attachment_link/#post-2263811)
 * A small code change and the problem is solved.
 * Changed:
    `the_attachment_link( $attachment->ID , true );` To `echo wp_get_attachment_image(
   $attachment->ID, 'large');`
 * Now it works!
    🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Function: the_attachment_link ()’ is closed to new replies.

## Tags

 * [attachment](https://wordpress.org/support/topic-tag/attachment/)
 * [image](https://wordpress.org/support/topic-tag/image/)
 * [the_attachment_link](https://wordpress.org/support/topic-tag/the_attachment_link/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [MDavid-ITA](https://wordpress.org/support/users/mdavid-ita/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/function-the_attachment_link/#post-2263811)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
