Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to display the image attachment description WordPress

  • SOLVED

Hello, this is my first time posting...

I have a php snippet that displays the post attachments and I would like to add a line that will echo the attachment description from the wordpress image uploader.

Here is the code so far....


<?php
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => -1,
);
$meta = wp_get_attachment_metadata($attachment->ID);

$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo "<a class='thumbnail' rel='shadowbox[post-";
echo the_ID();
echo "]' href='";
echo wp_get_attachment_url($attachment->ID);
echo "' title='";
//echo meta;
echo "' >";
echo wp_get_attachment_image($attachment->ID, 'thumbnail', false, false);
echo "</a>";
}
}
?>


Where the above code says <strong>echo meta</strong> I would like to display the description for the attachment. I thought it might have to do <strong>wp_get_attachment_metadata();</strong> but this did not work.

Thanks in advance to anyone who can help, it is much appreciated!

Cheers,
Matt

Answers (2)

2010-12-14

Michael Fields answers:

The description is not stored in the meta. It is stored as post content in the attachments object. Something like this should work:
$attachment_id = 12345;
$attachment = get_post( $attachment_id );
print $attachment->post_content;


Best wishes,
-Mike


Michael Fields comments:

Here are a couple improvements to the loop you posted:

$backup = $post; /* Backup global post object. */
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $post ) {
setup_postdata( $post ); /* Allows you to use more template tags. */

/* You should now be able to use the_content(); */
the_content();

echo "<a class='thumbnail' rel='shadowbox[post-";
echo the_ID();
echo "]' href='";
echo wp_get_attachment_url($attachment->ID);
echo "' title='";
//echo meta;
echo "' >";
echo wp_get_attachment_image($attachment->ID, 'thumbnail', false, false);
echo "</a>";
}
}
$post = $backup; /* Restore global post object. */


Michael Fields comments:

By the way, if you are using the value of post_content inside the title attribute, you will want to use something like:

print esc_attr( get_the_content() );

2010-12-14

Victor Petrescu answers:

Just add:
<?php the_content(); ?> where you need the description displayed.