Title: Get Attachment outside the Loop
Last modified: August 20, 2016

---

# Get Attachment outside the Loop

 *  [wannalearnthis](https://wordpress.org/support/users/wannalearnthis/)
 * (@wannalearnthis)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/get-attachment-outside-the-loop/)
 * I’ve been trying to develop a very simple task, but the information online seems
   rather confusing.
 * I modified the query to display a single post per-page in the loop (each post
   will display a photo attachment), my intention is to use the same picture in 
   a background on a div container outside of the loop.
 * So basically I need to fetch the data from a single loop in my index and place
   the same data in the header/footer.
 * The reason why I want to do it this way is so that pagination still works and
   the information in the header/footer will always refresh on each page.

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

 *  Thread Starter [wannalearnthis](https://wordpress.org/support/users/wannalearnthis/)
 * (@wannalearnthis)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/get-attachment-outside-the-loop/#post-2771708)
 * This is the code I have before the main loop:
 *  Thread Starter [wannalearnthis](https://wordpress.org/support/users/wannalearnthis/)
 * (@wannalearnthis)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/get-attachment-outside-the-loop/#post-2771709)
 *     ```
       $post_args = array(
   
       	'posts_per_page' => 1,
       	'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
       	'orderby' => 'date',
       		);
   
       query_posts($post_args, $wp_query->query);
       ```
   
 *  Thread Starter [wannalearnthis](https://wordpress.org/support/users/wannalearnthis/)
 * (@wannalearnthis)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/get-attachment-outside-the-loop/#post-2771743)
 * I ended up doing something like this:
 * (However, I feel like the function is redundant, if anyone has a better idea,
   please share)
 *     ```
       $grid_src = get_bloginfo('stylesheet_directory') . '/library/images/ergos_grid_01.svg';
       $dynamic_grid = 'style="background-image:url(' . $grid_src . ');"';
   
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       $the_query = new WP_Query('posts_per_page=1'.'&paged='.$paged); //display X posts per page
   
       	// The Loop
       	if ($the_query->have_posts()) : while ( $the_query->have_posts()) : $the_query->the_post();
   
       		$thumbnail_src = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
       		$dynamic_background = 'style="background-image: url(' . $grid_src . '), url(' . $thumbnail_src . ') ;"';
       		echo $dynamic_background;
   
       	endwhile;
   
       	else:
       		echo 'no backgrounds found';
       	endif;
   
       // Reset Post Data - Best used after custom or multiple loops created with WP_Query
       	wp_reset_postdata();
       ```
   
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/get-attachment-outside-the-loop/#post-2771817)
 * When that page loads you should have a `$posts` variable already populated. `
   $posts[0]` should have most of the information you need. That is, it should have
   the ‘post’ data for the first post in the loop, which is the only one you are
   showing as per you description. `wp_get_attachment_url( get_post_thumbnail_id(
   $post->ID) );`, or something very similar, should get you your image. I don’t
   think you need to run a new query. Not tested but I think this will work.
 *  Thread Starter [wannalearnthis](https://wordpress.org/support/users/wannalearnthis/)
 * (@wannalearnthis)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/get-attachment-outside-the-loop/#post-2771891)
 * Thanks s_ha_dum!
 * using `$posts[0]` helps me to get the image and other content in the first page,
   but the code breaks after going to the next page.
 * MAYBE I FORGOT TO WRITE AN IMPORTANT PIECE OF INFORMATION: I AM WORKING ON THE
   INDEX PAGE, NOT SINGLE OR STATIC PAGE.
 * I want the function to be dynamic and update with the loop, including pagination.
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/get-attachment-outside-the-loop/#post-2771924)
 * So you know, I’ve never actually done this myself. I am guessing all the way 
   along and I don’t have the time right now to do any testing.
 * What do you mean by ‘next page’? Do you mean the next post in the list or are
   you talking about the next page of a [paginated post](http://wordpress.org/support/topic/creating-paginated-posts?replies=4)?
 * In what way does it break? Do you get an error? A warning? What happens?
 *  Thread Starter [wannalearnthis](https://wordpress.org/support/users/wannalearnthis/)
 * (@wannalearnthis)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/get-attachment-outside-the-loop/#post-2771956)
 * I would say both, because I modified the query to display only one post, so next
   post is also next page.
 * However, yes, I am referring to a paginated post.
 * The code doesn’t necessarily break as of to give me an error, the outcome of 
   going to the next page (paginated post) is that the background ($posts[0]) simply
   disappears.
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/get-attachment-outside-the-loop/#post-2771958)
 * `var_dump($posts);` in your template, then load both pages and see what changes.

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

The topic ‘Get Attachment outside the Loop’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 2 participants
 * Last reply from: [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/get-attachment-outside-the-loop/#post-2771958)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
