0

I am trying to get the text stored in an image caption on WordPress. I have code that gets the images attached to a post and with a second bit of code I can display them later in the page.

This is what gets the images attached to the post.

<?php
              if ($attachments = get_children(array(
                  'post_type' => 'attachment',
                  'post_mime_type' => 'image',
                  'numberposts' => -1,
                  'post_status' => null,
                  'post_parent' => $post->ID
                      )));

              foreach ($attachments as $attachment) {

                  $mynewarray = wp_get_attachment_image_src($attachment->ID, 'full');
                  $anotherarray [] = $mynewarray[0];
              } ?>

And this displays each image, I just change the [2] to call different images.

<?php echo $anotherarray[2]; ?>

All works well, but i want to get the text stored in the caption, I started this a few months ago and spend days trying to get the caption, gave up and just coming back to this now. So lost as, any help appreciated.

Thanks for the suggestions, but nothing has worked yet, I think this its coz I am calling all images attached to a post as an array. And then using the array parts to set the images as a background to a div.The method in the post listed as a smiler question does not work in this case.

Snowman
  • 1
  • 2

2 Answers2

0

Each attachment has its own row in the posts table. That row's post_excerpt column contains the caption.

So this will get the attachment's caption assuming you know the attachment's id.

$attachment = get_post( $attachment_id );
$caption = $attachment->post_excerpt;

Read this for more details, especially about the ALT tag which comes from post_meta.

https://wordpress.org/ideas/topic/functions-to-get-an-attachments-caption-title-alt-description

Edit

You seem to have a loop in your code. Can't you grab each attachment's post_excerpt like this? Unless there's something going terribly wrong this should work.

foreach ( $attachments as $attachment ) {
   $att = get_post( $attachment->ID );
   $caption = $att->post_excerpt;
   /*... */
   $mynewarray = wp_get_attachment_image_src($attachment->ID, 'full');
   $anotherarray [] = $mynewarray[0];
} 

You could also try get_post_field. That one's nice because it will crunch the contents the field so it's suitable for html display.

foreach ( $attachments as $attachment ) {
    $caption = get_post_field( 'post_excerpt', $attachment->ID );
     /* ... */
}
O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Thank for your help Ollie, This hasn't worked, or maybe i should say I haven't got this to work. I am using the first part of my code to call all images attached to a post as an array and then use the second part of code to set the background images of a div (3 div's in this case).I want to to display the caption text inside them div's. – Snowman Apr 14 '15 at 11:04
  • Thank Ollie, I am slowly seeing where your going with this. I have tried what you have suggested and haven't got it yet (100% my fault Id say). Can you tell me with your method how would i then display the caption, like i echo $anotherarray[0] to give me the image url. Does this method return me an array of the captions. – Snowman Apr 14 '15 at 16:32
0

Have a look at this function wp_prepare_attachment_for_js($attachment_id) - link

It will return these fields :

id
title
filename
url
link
alt
author
description
caption
name
status
uploadedTo
date
modified
menuOrder
mime
type
subtype
icon
dateFormatted
nonces
editLink
sizes
width
height
fileLength
compat
Raja Khoury
  • 3,015
  • 1
  • 20
  • 19