Title: how do I reference an uploaded post image using code?
Last modified: August 19, 2016

---

# how do I reference an uploaded post image using code?

 *  Resolved [ibexy](https://wordpress.org/support/users/ibexy/)
 * (@ibexy)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/)
 * I am building a custom posts + excerpt listing. I dont know how to pull uploaded/
   attached post images. Any help please?

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

 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/#post-1243668)
 * Assuming this is what you want…
    [http://codex.wordpress.org/Function_Reference/get_children](http://codex.wordpress.org/Function_Reference/get_children)
 *  [shane-g](https://wordpress.org/support/users/shane-g/)
 * (@shane-g)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/#post-1243687)
 * Hi,
 * Have a check with this plugin:
 * [http://wordpress.org/extend/plugins/image-link/](http://wordpress.org/extend/plugins/image-link/)
 * Thanks,
 * Shane G.
 *  Thread Starter [ibexy](https://wordpress.org/support/users/ibexy/)
 * (@ibexy)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/#post-1243902)
 * Thanks
    I found the following code but it returns every image from the gallery
   and not ONLY THE IMAGE attached to the current post. This is my code below. I
   want to return only the image assiciated with the current post in the loop.Basically
   the below prints an excerpt and am triying to extract the attached image to be
   like a thumbnail. Any help please?
 *     ```
       <?php $excerpt = get_the_excerpt();
                   echo $excerpt; ?>   
   
       <?php $thumbnailimages =& get_children( 'post_type=attachment&post_mime_type=image' );
   
                       if ( empty($thumbnailimages) ) {
                         // no attachments here
                       } else {
   
                         foreach ( $thumbnailimages as $attachment_id => $attachment ) {
                           echo wp_get_attachment_image( $attachment_id, 'full' );
                         }
                       }
                 ?>
       ```
   
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/#post-1243923)
 * See the parameters on the link i gave you..
    [http://codex.wordpress.org/Function_Reference/get_children](http://codex.wordpress.org/Function_Reference/get_children)
 * Namingly post_parent and numberposts..
 * Attachments are children of the post they exist in, so feed the post ID into 
   the post parent parameter..
 *  Thread Starter [ibexy](https://wordpress.org/support/users/ibexy/)
 * (@ibexy)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/#post-1243932)
 * Thanks for your response. Am a bit lost with all this tech. talk 🙂 I’d really
   appreaciate if you could help further. I have inserted my full archive.php here
   hoping you could help. Currently this file lists the excerpts. I just need to
   insert a thumbnail of the attached images using the above or other means. Thanks.
 *     ```
       <?php get_header(); ?>
       <?php get_sidebar(); ?>
       <div class="archive2">
         <?php $odd_or_even = 'odd'; ?>
         <?php if (have_posts()) : ?>
         <?php while (have_posts()) : the_post(); // check for product large image
       $pli = get_post_meta($post->ID, 'pli', $single = true); // check for item price
       $price = get_post_meta($post->ID, 'price', $single = true);
        ?>
         <div class="post-<?php echo $odd_or_even; ?>" id="post-<?php the_ID(); ?>">
           <?php $odd_or_even = ('odd'==$odd_or_even) ? 'even' : 'odd'; ?>
           <div class="col1">
             <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php echo THEME_PERMANENT_LINK; ?><?php the_title_attribute(); ?>">
               <?php the_title(); ?>
               </a></h2>
   
       <!-- THIS IS THE EXCERPT HERE -->
   
                   <?php $excerpt = strip_tags(get_the_excerpt());
                   echo $excerpt; ?>           
   
             <p><img src="<?php bloginfo('template_directory'); ?>/imgs/page.png" alt="<?php echo THEME_CATEGORIES; ?>" /> <strong><?php echo THEME_CATEGORIES; ?></strong>
               <?php the_category(' '); ?>
             <p>
             <p><img src="<?php bloginfo('template_directory'); ?>/imgs/tag.png" alt="<?php echo THEME_TAGS; ?>" /> <strong><?php echo THEME_TAGS; ?></strong>
               <?php the_tags(' '); ?>
             <p>
           </div>
   
           <div style="clear: both"></div>
         </div>
         <?php endwhile; ?>
         <div class="navigation">
           <div class="left">
             <?php next_posts_link(THEME_POST_ENTRIES_PREV) ?>
           </div>
           <div class="right">
             <?php previous_posts_link(THEME_POST_ENTRIES_NEXT) ?>
           </div>
         </div>
         <?php else : ?>
         <h2 class="center"><?php echo THEME_POST_NOT_FOUND; ?></h2>
         <p class="center"><?php echo THEME_POST_NOT_FOUND_MSG; ?></p>
         <?php include (TEMPLATEPATH . "/searchform.php"); ?>
         <?php endif; ?>
       </div>
   
       <?php get_footer(); ?>
       ```
   
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/#post-1243936)
 * Try this..
 *     ```
       <?php get_header(); ?>
       <?php get_sidebar(); ?>
       <div class="archive2">
       	<?php $odd_or_even = 'odd'; ?>
       	<?php if (have_posts()) : ?>
       	<?php while (have_posts()) : the_post(); // check for product large image
       	$pli = get_post_meta($post->ID, 'pli', true); // check for item price
       	$price = get_post_meta($post->ID, 'price', true);
       	?>
       	<div class="post-<?php echo $odd_or_even; ?>" id="post-<?php the_ID(); ?>">
       		<?php $odd_or_even = ('odd'==$odd_or_even) ? 'even' : 'odd'; ?>
       		<div class="col1">
       			<h2>
       				<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php echo THEME_PERMANENT_LINK; ?><?php the_title_attribute(); ?>">
       				<?php the_title(); ?>
       				</a>
       			</h2>
       			<?php
       			$args = array(
       				'post_parent' => $post->ID,
       				'post_type' => 'attachment',
       				'numberposts' => 1,
       				'post_status' => 'any',
       				'post_mime_type' => 'image'
       			);
       			$thumbnailimages =&get_children( $args );
       			if ( !empty($thumbnailimages) ) {
       				foreach ( $thumbnailimages as $attachment_id => $attachment ) echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
       			}
       			?>
       			<!-- THIS IS THE EXCERPT HERE -->
       			<?php echo strip_tags(get_the_excerpt()); ?>
       			<p><img src="<?php bloginfo('template_directory'); ?>/imgs/page.png" alt="<?php echo THEME_CATEGORIES; ?>" /> <strong><?php echo THEME_CATEGORIES; ?></strong>
       			  <?php the_category(' '); ?>
       			</p>
       			<p><img src="<?php bloginfo('template_directory'); ?>/imgs/tag.png" alt="<?php echo THEME_TAGS; ?>" /> <strong><?php echo THEME_TAGS; ?></strong>
       			  <?php the_tags(' '); ?>
       			</p>
       		</div>
       		<div style="clear: both"></div>
       	</div>
       	<?php endwhile; ?>
       	<div class="navigation">
       		<div class="left"><?php next_posts_link(THEME_POST_ENTRIES_PREV) ?></div>
       		<div class="right"><?php previous_posts_link(THEME_POST_ENTRIES_NEXT) ?></div>
       	</div>
       	<?php else : ?>
       	<h2 class="center"><?php echo THEME_POST_NOT_FOUND; ?></h2>
       	<p class="center"><?php echo THEME_POST_NOT_FOUND_MSG; ?></p>
       	<?php // include (TEMPLATEPATH . "/searchform.php"); ?>
       	<?php get_search_form(); // Does the same as above line ?>
       	<?php endif; ?>
       </div>
       <?php get_footer(); ?>
       ```
   
 *  Thread Starter [ibexy](https://wordpress.org/support/users/ibexy/)
 * (@ibexy)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/#post-1243947)
 * it worked perfectly! Thanks very much!
 * I added a <div> around the image to enable me add a float:left CSS. Its showing
   OK except a few images are not coming up. Dont know why but am ok with this result.
 * How do I scale all the images to say a uniform 120×120. Is ther any way to do
   this in CSS? I have tried the width and height statements. They dont work.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/#post-1243969)
 * Set the thumbnail size to 120 x 120?
 * If you have uploaded images already you’ll need to re-add them again under the
   new settings.
 *  Thread Starter [ibexy](https://wordpress.org/support/users/ibexy/)
 * (@ibexy)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/#post-1243972)
 * Thanks.

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

The topic ‘how do I reference an uploaded post image using code?’ is closed to new
replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 3 participants
 * Last reply from: [ibexy](https://wordpress.org/support/users/ibexy/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/how-do-i-reference-an-uploaded-post-image-using-code/#post-1243972)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
