July, 2009


090723

Get URLs, Captions & Titles for Images Attached to Posts in WordPress

I've been working on a new WordPress theme for my client that involves a billboard concept where certain posts are categorized as homepage billboard items, and those post items have images attached to them within the Media gallery of WordPress. I found out how to pull out the URI address of the images that are attached to the posts, but it took me some time to figure out how to get the Title, Caption and Description text associated directly with that image attachment. I borrowed most of my code from here, but I think my example illustrates the concept of retrieving the data needed much easier.

General Code Concept

$size = "thumbnail"; // options: thumbnail, medium, large, full
$args = array(
	'numberposts' => null, // change this to a specific number of images to grab
	'post_parent' => '25',
	'post_type' => 'attachment',
	'nopaging' => false,
	'post_mime_type' => 'image',
	'order' => 'ASC', // change this to reverse the order
	'orderby' => 'menu_order ID', // select which type of sorting
	'post_status' => 'any'
);
$attachments =& get_children($args);
if ($attachments) {
	foreach($attachments as $attachment) {
		foreach($attachment as $attachment_key => $attachment_value) {
			$imageID = $attachment->ID;
			$imageTitle = $attachment->post_title;
			$imageCaption = $attachment->post_excerpt;
			$imageDescription = $attachment->post_content;
			$imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true);
			// $imageAlt = $attachment->image_alt; // not sure about this one
			$imageArray = wp_get_attachment_image_src($attachment_value, $size, false);
			$imageURI = $imageArray[0]; // 0 is the URI
			$imageWidth = $imageArray[1]; // 1 is the width
			$imageHeight = $imageArray[2]; // 2 is the height
			echo "$imageURI \n";
			echo "$imageWidth \n";
			echo "$imageHeight \n";
			echo "$imageID \n";
			echo "$imageTitle \n";
			echo "$imageCaption \n";
			echo "$imageDescription \n";
			if(count($imageAlt)) {
				echo $imageAlt;
			}// if '_wp_attachment_image_alt' is not set, echo $alt; shows text like 'Array()'
			break;
		}
	}
}
unset($args); // clear out arguments if using code multiple times in a theme

Practical Usage #1

Use the following to iterate all images from a post Hint: can use the above example's variables to pull additional info for each.
$size = "medium";
$args = array(
	'numberposts' => null,
	'post_parent' => $post->ID,
	'post_type' => 'attachment',
	'nopaging' => false,
	'post_mime_type' => 'image',
	'order' => 'ASC',
	'orderby' => 'menu_order ID',
	'post_status' => 'any'
);
$attachments =& get_children($args);
if ($attachments) {
	foreach($attachments as $attachment) {
		foreach($attachment as $attachment_key => $attachment_value) {
			$imageID = $attachment->ID;
			$imageTitle = $attachment->post_title;
			$imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
			$imageURI = $imagearray[0]; // 0 is the URI
			$imageWidth = $imagearray[1]; // 1 is the width
			$imageHeight = $imagearray[2]; // 2 is the height
			echo "\t<img src="$imageURI" width="$imageWidth" height="$imageHeight" border="0">\n";
			break;
		}
	}
}
unset($args);

Practical Usage #2

If you want to get a specific image ..
$size = "medium";
$args = array(
	'numberposts' => 2, //change this to the image you want
	'post_parent' => $post->ID,
	'post_type' => 'attachment',
	'nopaging' => false,
	'post_mime_type' => 'image',
	'order' => 'ASC',
	'orderby' => 'menu_order ID',
	'post_status' => 'any'
);
$attachments =& get_children($args);
if ($attachments) {
	foreach($attachments as $attachment) {
		foreach($attachment as $attachment_key => $attachment_value) {
			$imageID = $attachment->ID;
			$imageTitle = $attachment->post_title;
			$imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
			$imageURI = $imagearray[0]; // 0 is the URI
			$imageWidth = $imagearray[1]; // 1 is the width
			$imageHeight = $imagearray[2]; // 2 is the height
			// move this down below
			// echo "\t<img src="$imageURI" width="$imageWidth" height="$imageHeight" border="0">\n";
			break;
		}
	}
}
unset($args);
echo "\t<img src="$imageURI" width="$imageWidth" height="$imageHeight" border="0">\n";
The way the code above works is: the foreach loop, loops from the beginning and iterates through, but doesn't output .. until it's done the loop. It loops through 1, populating all of the variables, and then loops through 2, replacing all of the variables with 2's values. Because we changed the number of posts to retrieve, we only retrieve the last one we wanted, in this case the second image! This can also be changed to work backwards too (ie. we can get the "second last" image). This can be modified to work with any sort of suitable sorting option: alphanumerically, rating, by ID, etc. - all just by changing the orderby and order setting! Hope this solution works for your custom PHP queries and image retrieval via WordPress' Media gallery.
Share Button

090716

Permissions on Unix

I need to put this as a post on my site as I often forget what permission settings are and their corresponding numbers are for CHMOD and the like. 1 execute 2 write 3 execute+write 4 read 5 execute+read 6 write+read 7 execute+read+write
Share Button