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

Check if Caption and if not and a description show description WordPress


We post multiple attached images into wordpress posts and often as they are uploaded the data embedded in the image is imported into the description field not the caption. Sometimes we write a caption but clearly copying hundreds is a real pain when we do a lot of these image posts.

So I want to check if there is a caption and if there is then show it. If there is not one but there is a description then would like to display it where the caption would normally be.

Using Genesis framework.

Can see on this page

Here I have a caption

But on this page

I did not copy the description over to the caption in admin, so be easier if instead of having to do that it just checked and displayed a caption if there is one and if not then a description if there is one.

Answers (1)


Duncan O'Neill answers:


add this code to your loop;

$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'numberposts' => null, 'post_parent' => $post->ID );

$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;

Then find the place in your theme's directory ( probably in single.php ), with this markup, for the caption text;

<p class="wp-caption-text"> .... </p>

Within those two html tags, take out the existing code and or makup, and add;

if ($caption!=""){echo $caption;
} else {
echo $description;

From here;
[[LINK href=""]][[/LINK]]

Message me for more help if you can't find where in the theme's directory to add the code.