Title: Getting post image outside loop
Last modified: August 20, 2016

---

# Getting post image outside loop

 *  [dean-dolan](https://wordpress.org/support/users/dean-dolan/)
 * (@dean-dolan)
 * [14 years ago](https://wordpress.org/support/topic/getting-post-image-outside-loop/)
 * Using this outside the loop to display posts from a certain category. Tried everything
   I can find to try and get the post featured image to be used as the background
   but nothing seems to work.
 * Any idea how to make it?
 *     ```
       <?php query_posts('attachment&orderby=name&order=asc&cat=9&showposts=4'); ?>
       <?php while (have_posts()) : the_post(); ?>
       	<li style="background: url(<?php wp_get_attachment_image_src( $attachment_id, $size, $icon ); ?>)" ><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
       <?php endwhile;?>
       ```
   

Viewing 1 replies (of 1 total)

 *  [Tirumal](https://wordpress.org/support/users/tirumal/)
 * (@tirumal)
 * [14 years ago](https://wordpress.org/support/topic/getting-post-image-outside-loop/#post-2837234)
 * Store the values in variables like this.
 *     ```
       <?php query_posts('attachment&orderby=name&order=asc&cat=9&showposts=4'); ?>
       <?php while (have_posts()) : the_post(); ?>
       	<?php
               global $post;
       	$image_url = wp_get_attachment_image_src( $attachment_id, $size, $icon );
       	$permalink = get_permalink($post->ID);
       	$title = get_the_title($post->ID);
       	?>
       <?php endwhile;?>
       ```
   
 * and, use it outside like this.
 * `<li style="background: url(<?php echo $image_url; ?>)" ><a href="<?php echo 
   $permalink; ?>"><?php echo $title; ?></a></li>`

Viewing 1 replies (of 1 total)

The topic ‘Getting post image outside loop’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [featured image](https://wordpress.org/support/topic-tag/featured-image/)
 * [post](https://wordpress.org/support/topic-tag/post/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Tirumal](https://wordpress.org/support/users/tirumal/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/getting-post-image-outside-loop/#post-2837234)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
