<?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>Digging into WordPress &#187; images</title>
	<atom:link href="http://digwp.com/tag/images/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Take your WordPress skills to the next level.</description>
	<lastBuildDate>Fri, 18 May 2012 18:21:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Awesome Image-Attachment Recipes for WordPress</title>
		<link>http://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/</link>
		<comments>http://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 06:10:05 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[attachments]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[tags]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=458</guid>
		<description><![CDATA[Recently, I found myself on the front lines of WordPress&#8217; somewhat complicated Media-Library system. The site that I was developing required a rather elaborate system of retrieving and displaying image attachments. So, using the latest version of WordPress (2.8.3 at the time), I found myself experimenting with as many template tags and custom functions as [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I found myself on the front lines of WordPress&rsquo; somewhat complicated Media-Library system. The site that I was developing required a rather elaborate system of retrieving and displaying image attachments. So, using the latest version of WordPress (2.8.3 at the time), I found myself experimenting with as many template tags and custom functions as I could find. After much experimentation, I discovered the perfect solution, and along the way I collected a healthy collection of recipes for displaying image attachments and their various types of associated information.</p>
<p>In this <acronym title="Digging into WordPress">DiW</acronym> article, you will learn some tasty ways to include image-attachment information in your posts. From echoing the latest image path to displaying custom-sized image-links for all post attachments, this article should serve as an invaluable resource for anyone working with WordPress&rsquo; Media Library and its media-attachment functionality.</p>
<p><span id="more-458"></span></p>
<h3>Basic display of gallery attachments</h3>
<p>When displaying your images via the <code>[gallery]</code> shortcode, WordPress will display image-links for each image in the gallery. Each of these image-links points to the image-gallery page for that particular image. The image gallery is created by the <code>image.php</code> template if present in your theme files. Here is a basic way to display your gallery images from within the image-gallery loop:</p>
<pre><code>&lt;a href="&lt;?php echo wp_get_attachment_url($post-&gt;ID); ?&gt;"&gt;&lt;?php echo wp_get_attachment_image($post-&gt;ID, 'medium'); ?&gt;&lt;/a&gt;</code></pre>
<p>This code will display a medium-sized image-link to the original image. To display an image attachment of a different size, replace the <code>$size</code> parameter in the <code>wp_get_attachment_image()</code> template tag. Accepted values are <code>thumbnail</code>, <code>medium</code>, <code>large</code>, or <code>full</code>. The actual dimensions of these sizes are determined by your preferences in the Media panel of the WordPress Admin.</p>
<h3>Display the URL of the latest image attachment</h3>
<p>Perhaps the most useful template tag for displaying image-attachment information is <code>wp_get_attachment_url()</code>. This function returns a full <acronym title="Uniform Resource Identifier">URI</acronym> for an attachment file. If no attachment is found, a value of <code>false</code> is returned. Here are several ways to use this tag within the loop:</p>
<pre><code>&lt;?php // display the attachment URI for post with specified ID
echo wp_get_attachment_url(7); ?&gt;

&lt;?php // display the latest attachment URI for each post from within the image.php loop
echo wp_get_attachment_url($post-&gt;ID); ?&gt;

&lt;?php // display the latest attachment URI for each post from within the index.php loop
echo wp_get_attachment_url($attachment_id); ?&gt;</code></pre>
<p>The output of this tag is similar to the following for each attachment:</p>
<p><code>http://digwp.com/wp-content/uploads/2009/08/example.png</code></p>
<p>If you are using the <code>image.php</code> file to display your attachments, use the second recipe to display the latest <acronym title="Uniform Resource Identifier">URI</acronym> for each post. If you are displaying your attachments from any other template file (e.g., <code>index.php</code>, <code>single.php</code>, etc.), use the third recipe and refer to <a href="#dynamic-post-id" title="Jump!">this section</a> of the article for the code required to generate dynamically the <code>$attachment_id</code> variable for the post ID.</p>
<h3>Display the latest image attachment as an image</h3>
<p>The <code>wp_get_attachment_image()</code> template tag is used to display the latest image attachment as an actual <acronym title="Hypertext Markup Language">HTML</acronym> image element. If no image attachment is found, a value of <code>false</code> is returned. Here are several ways to use this tag within the loop:</p>
<pre><code>&lt;?php // display medium-sized attached image for post with specified ID
echo wp_get_attachment_image(7, 'medium'); ?&gt;

&lt;?php // display the latest attached image for each post from within the image.php loop
echo wp_get_attachment_image($post-&gt;ID); ?&gt;

&lt;?php // display the latest attached image for each post from within the index.php loop
echo wp_get_attachment_image($attachment_id); ?&gt;</code></pre>
<p>The output of this tag is similar to the following for each attachment:</p>
<p><code>&lt;img src="http://digwp.com/wp-content/uploads/2009/08/example.png" class="attachment-thumbnail" height="50" width="50"&gt;</code></p>
<p>The <code>wp_get_attachment_image()</code> template tag may be customized with the following parameters:</p>
<p><code>&lt;?php wp_get_attachment_image($attachment_id, $size='thumbnail', $icon=false); ?&gt;</code></p>
<ul>
<li><code>$attachment_id</code> &#8212; ID of the desired attachment. Not required if used within an attachment loop, otherwise, refer to <a href="#dynamic-post-id" title="Jump!">this section</a> to generate the ID for non-attachment loops.</li>
<li><code>$size</code> &#8212; Size of the image shown for an image attachment: <code>thumbnail</code>, <code>medium</code>, <code>large</code> or <code>full</code></li>
<li><code>$icon</code> &#8212; (Optional) Use a media icon to represent the attachment. Default value: <code>false</code>.</li>
</ul>
<h3>Display the thumbnail of the latest image attachment</h3>
<p>I couldn&rsquo;t find any official documentation for the <code>get_attachment_icon()</code> template tag, but after some experimentation, it seems useful for displaying a thumbnail of the latest image attachment for each post. Here are some examples:</p>
<pre><code>&lt;?php // display image thumbnail for post with specified ID
echo get_attachment_icon(7); ?&gt;

&lt;?php // display image thumbnail for each post from within the image.php loop
echo get_attachment_icon($post-&gt;ID); ?&gt;

&lt;?php // display image thumbnail for each post from within the index.php loop
echo get_attachment_icon($attachment_id); ?&gt;</code></pre>
<p>The output of this tag is similar to the following for each attachment:</p>
<p><code>&lt;img src="http://digwp.com/wp-content/uploads/2009/08/example.png" title="example" alt="example"&gt;</code></p>
<p>If using this tag within an attachment loop, such as in the <code>image.php</code> or <code>attachment.php</code> theme file, the post ID parameter is generated automatically. If using within a non-attachment loop, such as in the <code>index.php</code> or <code>single.php</code> theme file, refer to <a href="#dynamic-post-id" title="Jump!">this section</a> for the code required to generate dynamically the post-ID variable.</p>
<h3>Other useful template tags for displaying image attachments</h3>
<p>Here are some other useful template tags for displaying image attachments and their related information:</p>
<pre><code>&lt;?php // returns the URL for the latest attachment thumbnail
wp_get_attachment_thumb_url($attachment_id); ?&gt;

&lt;?php // returns an image representing the latest attachment file
wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon=false); ?&gt;

&lt;?php // returns an image-link or text-link to the latest attachment file or attachment page
wp_get_attachment_link($id=0, $size='thumbnail', $permalink=false, $icon=false); 

&lt;?php // displays an image link to the latest attachment file
the_attachment_link($attachment_id); ?&gt;

&lt;?php // returns an image link to the latest attachment file
get_the_attachment_link($attachment_id);  ?&gt;

&lt;?php // returns a URI to the attachment page for the latest attachment
get_attachment_link($attachment_id);  ?&gt;</code></pre>
<p>See the next section for generating the values of the <code>$attachment_id</code> variable.</p>
<h3 id="dynamic-post-id">Dynamic generation of the post ID for non-attachment loops</h3>
<p>If you are using any of the above recipes within a <code>single.php</code>, <code>index.php</code>, or any other non-attachment loop, you will need to include the following line of code to generate the ID for each post:</p>
<pre><code>&lt;?php global $wpdb; $attachment_id = $wpdb-&gt;get_var("SELECT ID FROM $wpdb-&gt;posts WHERE post_parent = '$post-&gt;ID' 
AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1"); ?&gt;</code></pre>
<p>This code dynamically generates the <code>$attachment_id</code> variable that is used in some of the examples above. Here is an example showing how to display the latest attachment <acronym title="Uniform Resource Identifier">URI</acronym> for each post from within the <code>index.php</code> loop:</p>
<pre><code>&lt;?php global $wpdb; $attachment_id = $wpdb-&gt;get_var("SELECT ID FROM $wpdb-&gt;posts WHERE post_parent = '$post-&gt;ID' 
AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1"); 
echo wp_get_attachment_url($attachment_id); ?&gt;</code></pre>
<p>Enough of the simple stuff! Let&rsquo;s move on to some more <em>advanced</em> recipes.</p>
<h3>Display all attachments for each post</h3>
<p>Up to this point, we have been using template tags to display information related to the <em>latest attachment</em> only. With any of the above methods, the output for any of the template tags represents a <em>single</em> attachment item. In order to display <em>multiple</em> attachments for each post, we need to employ WordPress&rsquo; <code>get_children()</code> functionality. The <code>get_children()</code> tag returns an associative array of posts with post IDs as array keys. The default parameters are as follows (as of version 2.7):</p>
<pre><code>$defaults = array( 
	'post_parent' =&gt; 0,     // get children of this post ID; null value gets all children
	'post_type'   =&gt; 'any', // post type: attachment, page, revision, or any; default: any
	'numberposts' =&gt; -1,    // number of child posts to retrieve; default: -1 (unlimited)
	'post_status' =&gt; 'any', // post status: publish, draft, inherit, or any; default: any
);</code></pre>
<p>For (cough) complete information (cough), refer to the <a href="http://codex.wordpress.org/Function_Reference/get_children" title="WordPress Codex: get_children()">official documentation</a>.</p>
<p>Using this functionality, we are well-equipped to use any of the previously discussed template tags (or any other attachment-related tag) to display all of the attachments for each post in the loop. Here is the basic technique, using the <code>wp_get_attachment_link()</code> template tag:</p>
<pre><code>&lt;?php 
$args = array(
	'order'          =&gt; 'ASC',
	'post_type'      =&gt; 'attachment',
	'post_parent'    =&gt; $post-&gt;ID,
	'post_mime_type' =&gt; 'image',
	'post_status'    =&gt; null,
	'numberposts'    =&gt; -1,
);
$attachments = get_posts($args);
if ($attachments) {
	foreach ($attachments as $attachment) {
		echo apply_filters('the_title', $attachment-&gt;post_title);
		echo wp_get_attachment_link($attachment-&gt;ID, 'thumbnail', false, false);
	}
}
?&gt;</code></pre>
<p>When used within the loop, this code will output the title and thumbnail-size image-link for <em>every</em> attachment of each post within the loop. As is, the image-links point to the <acronym title="Uniform Resource Identifier">URI</acronym> of the original, full-size attachment file. To get the image-links to point to the attachment page for each image, change the third parameter in the <code>wp_get_attachment_link()</code> tag to <code>true</code>. </p>
<p>Currently, the function will display <em>all</em> attachments for each post. To limit the number of attachments displayed, change the <code>numberposts</code> value from &ldquo;<code>-1</code>&rdquo; to whatever number you wish.</p>
<p>Once you get the hang of this basic attachment-display method, you can have some fun experimenting with different output formats by swapping out and/or including additional template tags. For example, instead of displaying a thumbnail link along with the attachment title, you may want to display the <acronym title="Uniform Resource Identifier">URI</acronym> of the original attachment. To do so, you would simply replace this line:</p>
<p><code>echo wp_get_attachment_link($attachment-&gt;ID, 'thumbnail', false, false);</code></p>
<p>..with this:</p>
<p><code>wp_get_attachment_url($attachment-&gt;ID)</code></p>
<p>Now, what about all of the other tags? How to implement and integrate them into a cohesive configuration of image-attachment bliss? There are far too many possible configurations to even begin to cover them all, but I did come up with an effective way of demonstrating the configurational possibilities with a little function I like to call the &ldquo;Attachment&nbsp;Toolbox&rdquo;.</p>
<h3>Image-attachment enlightenment with the Attachment Toolbox</h3>
<p>The idea is simple: <strong>learn by example</strong>. This function is essentially a &ldquo;live&rdquo; demonstration of the various template tags and their generated output. To use this function, create a few posts and attach some images (or other types of attachments) to each of them. Then, place the following code into your theme&rsquo;s <code>functions.php</code> file:</p>
<pre><code>&lt;?php 
function attachment_toolbox($size = thumbnail) {

	if($images = get_children(array(
		'post_parent'    =&gt; get_the_ID(),
		'post_type'      =&gt; 'attachment',
		'numberposts'    =&gt; -1, // show all
		'post_status'    =&gt; null,
		'post_mime_type' =&gt; 'image',
	))) {
		foreach($images as $image) {
			$attimg   = wp_get_attachment_image($image-&gt;ID,$size);
			$atturl   = wp_get_attachment_url($image-&gt;ID);
			$attlink  = get_attachment_link($image-&gt;ID);
			$postlink = get_permalink($image-&gt;post_parent);
			$atttitle = apply_filters('the_title',$image-&gt;post_title);

			echo '&lt;p&gt;&lt;strong&gt;wp_get_attachment_image()&lt;/strong&gt;&lt;br /&gt;'.$attimg.'&lt;/p&gt;';
			echo '&lt;p&gt;&lt;strong&gt;wp_get_attachment_url()&lt;/strong&gt;&lt;br /&gt;'.$atturl.'&lt;/p&gt;';
			echo '&lt;p&gt;&lt;strong&gt;get_attachment_link()&lt;/strong&gt;&lt;br /&gt;'.$attlink.'&lt;/p&gt;';
			echo '&lt;p&gt;&lt;strong&gt;get_permalink()&lt;/strong&gt;&lt;br /&gt;'.$postlink.'&lt;/p&gt;';
			echo '&lt;p&gt;&lt;strong&gt;Title of attachment&lt;/strong&gt;&lt;br /&gt;'.$atttitle.'&lt;/p&gt;';
			echo '&lt;p&gt;&lt;strong&gt;Image link to attachment page&lt;/strong&gt;&lt;br /&gt;&lt;a href="'.$attlink.'"&gt;'.$attimg.'&lt;/a&gt;&lt;/p&gt;';
			echo '&lt;p&gt;&lt;strong&gt;Image link to attachment post&lt;/strong&gt;&lt;br /&gt;&lt;a href="'.$postlink.'"&gt;'.$attimg.'&lt;/a&gt;&lt;/p&gt;';
			echo '&lt;p&gt;&lt;strong&gt;Image link to attachment file&lt;/strong&gt;&lt;br /&gt;&lt;a href="'.$atturl.'"&gt;'.$attimg.'&lt;/a&gt;&lt;/p&gt;';
		}
	}
}
?&gt;</code></pre>
<p>Then, in your <code>index.php</code> or <code>single.php</code> (or other non-attachment) loop, call the function with the following tag:</p>
<pre><code>&lt;?php attachment_toolbox('thumbnail'); ?&gt;</code></pre>
<p>Then check the results in your browser. You will see that the code has output a variety of different attachment data, including title, attachment URI, post URI, image URI, image thumbnail, image links, and so on. After examining the output, refer back to the function itself to understand the functionality of each tag. Once you begin to see the correlation between source code and page output, you will be well equipped to transform the function in any way you see fit, or even use it as a guide to create your own custom attachment-display function. It&rsquo;s all there, just dig in and check it out&nbsp;:)</p>
<h3>Check please</h3>
<p>That does it for this fun-filled <acronym title="Digging into WordPress">DiW</acronym> article. We have seen how to display many different types of image-attachment information, including everything from thumbnail links and <acronym title="Uniform Resource Locator">URL</acronym> paths to custom-sized images and multiple attachments. This collection of recipes is by no means exhaustive, but it provides plenty of key techniques to help implement and customize your own image-attachment functionality. Working with WordPress&rsquo; Media Library can be a convoluted process, to say the least, so any tips and tricks that you happen to know will be greatly appreciated by the incredibly awesome WordPress community.</p>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/">Permalink</a> | <a href="http://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/#comments">46 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/&title=Awesome Image-Attachment Recipes for WordPress">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/attachments/" rel="tag">attachments</a>, <a href="http://digwp.com/tag/images/" rel="tag">images</a>, <a href="http://digwp.com/tag/tags/" rel="tag">tags</a>, <a href="http://digwp.com/tag/tips/" rel="tag">tips</a>, <a href="http://digwp.com/tag/tricks/" rel="tag">tricks</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
	</channel>
</rss>

