Skip to content

Instantly share code, notes, and snippets.

@jcanfield
Last active June 25, 2020 22:56
Show Gist options
  • Save jcanfield/4525929 to your computer and use it in GitHub Desktop.
Save jcanfield/4525929 to your computer and use it in GitHub Desktop.
Wordpress Image Attachment Recipes > Post by DigWP.com which can be found at http://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/. > Even though this is an older post there are a few very simple Wordpress functions and methods utilizing Post Images with-in your code.
/*****
* Basic display of gallery attachments
* When displaying your images via the [gallery] 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 image.php template if present in your theme files. Here is a basic way to display your gallery images from within the image-gallery loop:
*****/
<a href="<?php echo wp_get_attachment_url($post->ID); ?>"><?php echo wp_get_attachment_image($post->ID, 'medium'); ?></a>
/*****
* Display the URL of the latest image attachment
* Perhaps the most useful template tag for displaying image-attachment information is wp_get_attachment_url(). This function returns a full URI for an attachment file. If no attachment is found, a value of false is returned. Here are several ways to use this tag within the loop:
*****/
<?php // display the attachment URI for post with specified ID
echo wp_get_attachment_url(7); ?>
<?php // display the latest attachment URI for each post from within the image.php loop
echo wp_get_attachment_url($post->ID); ?>
<?php // display the latest attachment URI for each post from within the index.php loop
echo wp_get_attachment_url($attachment_id); ?>
/* The output of this tag is similar to the following for each attachment: http://digwp.com/wp-content/uploads/2009/08/example.png */
/*****
* Display the latest image attachment as an image
* The wp_get_attachment_image() template tag is used to display the latest image attachment as an actual HTML image element. If no image attachment is found, a value of false is returned. Here are several ways to use this tag within the loop:
*****/
<?php // display medium-sized attached image for post with specified ID
echo wp_get_attachment_image(7, 'medium'); ?>
<?php // display the latest attached image for each post from within the image.php loop
echo wp_get_attachment_image($post->ID); ?>
<?php // display the latest attached image for each post from within the index.php loop
echo wp_get_attachment_image($attachment_id); ?>
/*****
* Display the thumbnail of the latest image attachment
* I couldn’t find any official documentation for the get_attachment_icon() 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:
*****/
<?php // display image thumbnail for post with specified ID
echo get_attachment_icon(7); ?>
<?php // display image thumbnail for each post from within the image.php loop
echo get_attachment_icon($post->ID); ?>
<?php // display image thumbnail for each post from within the index.php loop
echo get_attachment_icon($attachment_id); ?>
/*****
* Other useful template tags for displaying image attachments
* Here are some other useful template tags for displaying image attachments and their related information:
*****/
<?php // returns the URL for the latest attachment thumbnail
wp_get_attachment_thumb_url($attachment_id); ?>
<?php // returns an image representing the latest attachment file
wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon=false); ?>
<?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);
<?php // displays an image link to the latest attachment file
the_attachment_link($attachment_id); ?>
<?php // returns an image link to the latest attachment file
get_the_attachment_link($attachment_id); ?>
<?php // returns a URI to the attachment page for the latest attachment
get_attachment_link($attachment_id); ?>
/*****
* Dynamic generation of the post ID for non-attachment loops
* If you are using any of the above recipes within a single.php, index.php, or any other non-attachment loop, you will need to include the following line of code to generate the ID for each post:
*****/
<?php global $wpdb; $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID'
AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1"); ?>
/* This code dynamically generates the $attachment_id variable that is used in some of the examples above. Here is an example showing how to display the latest attachment URI for each post from within the index.php loop: */
<?php global $wpdb; $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID'
AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
echo wp_get_attachment_url($attachment_id); ?>
/*****
* Display all attachments for each post
* Up to this point, we have been using template tags to display information related to the latest attachment only. With any of the above methods, the output for any of the template tags represents a single attachment item. In order to display multiple attachments for each post, we need to employ WordPress’ get_children() functionality. The get_children() tag returns an associative array of posts with post IDs as array keys. The default parameters are as follows (as of version 2.7):
*****/
<?php
$defaults = array(
'post_parent' => 0, // get children of this post ID; null value gets all children
'post_type' => 'any', // post type: attachment, page, revision, or any; default: any
'numberposts' => -1, // number of child posts to retrieve; default: -1 (unlimited)
'post_status' => 'any', // post status: publish, draft, inherit, or any; default: any
);
?>
/* 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 wp_get_attachment_link() template tag: */
<?php
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => -1,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo apply_filters('the_title', $attachment->post_title);
echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
}
}
?>
/* 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 URI of the original attachment. To do so, you would simply replace this line:
echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
..with this:
wp_get_attachment_url($attachment->ID)
*/
/*****
* Image-attachment enlightenment with the Attachment Toolbox
* The idea is simple: learn by example. This function is essentially a “live” 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’s functions.php file:
*****/
<?php
function attachment_toolbox($size = thumbnail) {
if($images = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1, // show all
'post_status' => null,
'post_mime_type' => 'image',
))) {
foreach($images as $image) {
$attimg = wp_get_attachment_image($image->ID,$size);
$atturl = wp_get_attachment_url($image->ID);
$attlink = get_attachment_link($image->ID);
$postlink = get_permalink($image->post_parent);
$atttitle = apply_filters('the_title',$image->post_title);
echo '<p><strong>wp_get_attachment_image()</strong><br />'.$attimg.'</p>';
echo '<p><strong>wp_get_attachment_url()</strong><br />'.$atturl.'</p>';
echo '<p><strong>get_attachment_link()</strong><br />'.$attlink.'</p>';
echo '<p><strong>get_permalink()</strong><br />'.$postlink.'</p>';
echo '<p><strong>Title of attachment</strong><br />'.$atttitle.'</p>';
echo '<p><strong>Image link to attachment page</strong><br /><a href="'.$attlink.'">'.$attimg.'</a></p>';
echo '<p><strong>Image link to attachment post</strong><br /><a href="'.$postlink.'">'.$attimg.'</a></p>';
echo '<p><strong>Image link to attachment file</strong><br /><a href="'.$atturl.'">'.$attimg.'</a></p>';
}
}
}
?>
/* Then, in your index.php or single.php (or other non-attachment) loop, call the function with the following tag: */
<?php attachment_toolbox('thumbnail'); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment