<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RLM &#187; Images</title>
	<atom:link href="http://www.rlmseo.com/blog/category/wordpress/images/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rlmseo.com</link>
	<description></description>
	<lastBuildDate>Tue, 31 Jan 2012 03:37:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Get Images Attached to a Post</title>
		<link>http://www.rlmseo.com/blog/get-images-attached-to-post/</link>
		<comments>http://www.rlmseo.com/blog/get-images-attached-to-post/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 18:02:44 +0000</pubDate>
		<dc:creator>jcrens8392</dc:creator>
				<category><![CDATA[Images]]></category>
		<category><![CDATA[post attachment]]></category>
		<category><![CDATA[thumbnails]]></category>

		<guid isPermaLink="false">http://www.rlmseo.com/?p=320</guid>
		<description><![CDATA[Ever wonder how to pull the images attached to a WordPress post, to display as thumbnails on your homepage for instance? Here's a fairly simple way to get it done.]]></description>
			<content:encoded><![CDATA[<p>When you upload an image (or other media file) through the Write Post or Write Page panel, WordPress remembers which post that file is attached to. This is a great feature if you want to pull some of the attached images for use as thumbnails on your blog&#8217;s homepage for instance &#8211; similar to the way the Tutorials and Blog indexes are set up on this site.</p>
<h2>Inside and Outside The Loop</h2>
<p>For my project, I needed to get the attached images for a page outside of The Loop, so in this tutorial I&#8217;ll show you both ways. First inside the loop, then I&#8217;ll show you the changes to make to get this done outside the loop.</p>
<p>Also, I&#8217;m trying a new strategy this time around&#8230; Instead of giving you the complete code and then going through each section step by step, I&#8217;m shortening this post a bit by only showing the complete code&#8230;but well-commented so you can see what&#8217;s going on. Let me know what you think about this strategy and if you&#8217;d prefer to see the step by step break down like I&#8217;ve done in previous tutorials.</p>
<h2>First, Inside The Loop</h2>
<p>One thing to keep in mind is that inside the loop, the <code>$post </code>object is set, and so we can access its various properties using the <code>$post->;PROPERTY</code> syntax. So we first need to create a new function in our functions.php file.</p>
<pre>function bdw_get_images() {

    // Get the post ID
    $iPostID = $post-&gt;ID;

    // Get images for this post
    $arrImages =&amp; get_children(&#039;post_type=attachment&amp;post_mime_type=image&amp;post_parent=&#039; . $iPostID );

    // If images exist for this page
    if($arrImages) {

        // Get array keys representing attached image numbers
        $arrKeys = array_keys($arrImages);

		/******BEGIN BUBBLE SORT BY MENU ORDER************
		// Put all image objects into new array with standard numeric keys (new array only needed while we sort the keys)
		foreach($arrImages as $oImage) {
			$arrNewImages[] = $oImage;
		}

		// Bubble sort image object array by menu_order TODO: Turn this into std &quot;sort-by&quot; function in functions.php
		for($i = 0; $i &lt; sizeof($arrNewImages) - 1; $i++) {
			for($j = 0; $j &lt; sizeof($arrNewImages) - 1; $j++) {
				if((int)$arrNewImages[$j]-&gt;menu_order &gt; (int)$arrNewImages[$j + 1]-&gt;menu_order) {
					$oTemp = $arrNewImages[$j];
					$arrNewImages[$j] = $arrNewImages[$j + 1];
					$arrNewImages[$j + 1] = $oTemp;
				}
			}
		}

		// Reset arrKeys array
		$arrKeys = array();

		// Replace arrKeys with newly sorted object ids
		foreach($arrNewImages as $oNewImage) {
			$arrKeys[] = $oNewImage-&gt;ID;
		}
		******END BUBBLE SORT BY MENU ORDER**************/

        // Get the first image attachment
        $iNum = $arrKeys[0];

        // Get the thumbnail url for the attachment
        $sThumbUrl = wp_get_attachment_thumb_url($iNum);

        // UNCOMMENT THIS IF YOU WANT THE FULL SIZE IMAGE INSTEAD OF THE THUMBNAIL
        //$sImageUrl = wp_get_attachment_url($iNum);

        // Build the &lt;img&gt; string
        $sImgString = &#039;&lt;a href=&quot;&#039; . get_permalink() . &#039;&quot;&gt;&#039; .
                            &#039;&lt;img src=&quot;&#039; . $sThumbUrl . &#039;&quot; width=&quot;150&quot; height=&quot;150&quot; alt=&quot;Thumbnail Image&quot; title=&quot;Thumbnail Image&quot; /&gt;&#039; .
                        &#039;&lt;/a&gt;&#039;;

        // Print the image
        echo $sImgString;
	}
}</pre>
<p>Next you just need to call the function from wherever you want the image to show up at.</p>
<pre>bdw_get_images();</pre>
<h3>A couple things to note about the above code:</h3>
<ol>
<li>We&#8217;re only getting the first attached image&#8217;s thumbnail. If we wanted to display all the attached images, we&#8217;d have to use a for-loop to loop through each of the values in <code>$arrKeys</code>.</li>
<li>The first attached image is the first image uploaded, and not the first image according to the order you&#8217;ve sorted them in the WordPress gallery admin. If you want to get the first image according to the gallery menu order, I&#8217;ve added the simple bubble sort above (commented out).</li>
<li>If you want the full-size image instead of the thumbnail, use <code>wp_get_attachment_url()</code> instead of <code>wp_get_attachment_thumb_url()</code>.</li>
<li>I&#8217;ve hard coded the width and height of the thumbnail to be 150px by 150px. You could add a width and height parameter to the function declaration and pass those parameters when calling the function if you need those numbers to be flexible.</li>
</ol>
<h2>How to Do It Outside the Loop</h2>
<p>The main difference to get this done outside the loop is that the <code>$post</code> object won&#8217;t be set (or rather it will be set, but can&#8217;t be used reliably), so we need to either know the post ID we want the images for, or we need to be on the post/page that we want the images for, but just outside of the loop for that post/page.</p>
<p>If we&#8217;re on the post/page but outside the loop (i.e. in the sidebar), we can use the <code>get_the_ID()</code> function to get the ID of the current post/page. So instead of <code>$iPostID = $post->;ID</code>, we could use <code>$iPostID = get_the_ID()</code>.</p>
<p>If we want to pull the images from a post/page other than the one we&#8217;re currently viewing, we&#8217;d have to pass the post ID of the post/page we want to the <code>bdw_get_images()</code> function as a parameter when calling the function.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rlmseo.com/blog/get-images-attached-to-post/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
	</channel>
</rss>

