Ask your WordPress questions! Pay money and get answers fast! (more info)

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

  • SOLVED

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

http://www.monstersandcritics.com/basler-fashion-show-dusseldorf-pictures/6/

Here I have a caption

But on this page
http://www.monstersandcritics.com/basler-fashion-show-dusseldorf-pictures/16/

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)

2014-02-18

Duncan O'Neill answers:

Hi,

add this code to your loop;

<?php
$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;


<?php
if ($caption!=""){echo $caption;
} else {
echo $description;
}
?>


From here;
[[LINK href="http://wordpress.org/support/topic/get-title-alt-or-caption-from-media-library-and-insert-into-theme"]]http://wordpress.org/support/topic/get-title-alt-or-caption-from-media-library-and-insert-into-theme[[/LINK]]

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