• Howdy, I’m working on this little bit of code that finds all of the images associated with a post and then displays them in the way I want them. I’m looking for a way to grab the caption (or title) to an image. Any help is appreciated!

    <?php
    $default_size = 'medium';
    $files = get_children("post_parent=$id&post_type=attachment&post_mime_type=image&order=ASC&orderby=menu_order ID");
    	if($files){
    	$keys = array_keys($files);
    		foreach ($keys as $value) {
    		$thumb=wp_get_attachment_image_src($value,$default_size);
    		$caption=?????????
            print "{url: '$thumb[0]', title: '$caption'},";
    		}
    	}
    ?>

    I’m looking for what code I could replace my ?????? above with to grab the caption for the image?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter thejester12

    (@thejester12)

    Man this forum moves fast… can anyone help?

    It may not be the cleanest way, but here’s how I did it:

    <?php /* get caption of the image */
    ob_start();
    the_content('', '');
    if(preg_match('/class="wp-caption-text">(.*)</',ob_get_contents(),$postCaptionArr)){
      ob_end_clean(); ?>
      $caption = $postCaptionArr[1];
    }else{ ob_end_clean(); } ?>

    hello,

    just replace the “????????” with this : $value->post_title;
    it should return you the post caption, that’s all.

    the additional text added to the attachment in the media browser could also be retrieve with this : $value->post_excerpt;

    Worked great.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to Grab an Images Caption in theme?’ is closed to new replies.