0

I have this code to retrieve the image url for the images in a gallery and it works ok but i cannot figure out how i could get the caption for each image.

I tried searching all over but i cannot seem to put all the information out there together! Any suggestions on how i could retrieve the captions?

 function show_related_gallery_image_urls( $content ) {

    global $post;

    // Only do this on singular items
    if( ! is_singular() )
        return $content;

    // Make sure the post has a gallery in it
    if( ! has_shortcode( $post->post_content, 'gallery' ) )
        return $content;



    // Retrieve all galleries of this post
    $galleries = get_post_galleries_images( $post );

    $image_list = <<<END
                    <div class="side_bar">
            <div class="related">
                <h3>Related Images</h3>
END;

    // Loop through all galleries found
    foreach( $galleries as $gallery ) {

        // Loop through each image in each gallery
        foreach( $gallery as $image ) {

            $src = $image;

            $image_list .=  '<a href="' . $src . '" rel="' . get_the_title() . '">' 
                                . '<img src="' . $src .'" />' 
                            . '</a>';

        }

    }

    $image_list .= '</div></div>';

    // Append our image list to the content of our post
    $content .= $image_list;

    return $content;

 }
 add_filter( 'the_content', 'show_related_gallery_image_urls' );

I hope i explained myself well! Thanks!

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
user3540380
  • 3
  • 1
  • 2
  • Possible duplicate of [Is there a function to get the caption for an image in wordpress](http://stackoverflow.com/questions/1616281/is-there-a-function-to-get-the-caption-for-an-image-in-wordpress) – Gerald Schneider Jun 12 '16 at 15:32

1 Answers1

1

This hasn't been tested by try it, I cleaned up some of your code a bit:

1) Combined the first 2 IF statements into 1

2) Used get_post_gallery() (Codex) which returns the src and the image ID. We use the image ID to return the caption, we can also get the description and more if we needed to.

3) Removed the containing Foreach Statement since both my method only returns 1 gallery, not multiple so no need to loop through.

function show_related_gallery_image_urls( $content ) {
    global $post;

    // Only do this on singular items 
    if( ! is_singular() || !has_shortcode( $post->post_content, 'gallery' ) )
        return $content;

    // Retrieve all galleries of this post
    $galleries = get_post_gallery( $post, false );

    $image_list = <<<END
                    <div class="side_bar">
            <div class="related">
                <h3>Related Images</h3>
END;

    // Loop through each image in each gallery
    $i = 0; // Iterator
    foreach( $gallery['src'] as $src ) {
        $caption = wp_get_attachment($gallery['id'][$i])['caption'];

        $image_list .=  '<a href="' . $src . '" rel="' . get_the_title() . '">' 
                        . '<img src="' . $src .'" />'
                        . '<div class="caption">'
                            . $caption
                        . '</div>'
                    . '</a>';

        $i++; // Incremenet Interator
    }

    $image_list .= '</div></div>';

    // Append our image list to the content of our post
    $content .= $image_list;

    return $content;

 }
 add_filter( 'the_content', 'show_related_gallery_image_urls' );

function wp_get_attachment( $attachment_id ) {

    $attachment = get_post( $attachment_id );
    return array(
        'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
        'caption' => $attachment->post_excerpt,
        'description' => $attachment->post_content,
        'href' => get_permalink( $attachment->ID ),
        'src' => $attachment->guid,
        'title' => $attachment->post_title
    );
}

On a sidenote, there is a gallery filter function where you can change how the gallery is displayed post_gallery Filter, here's a question that kind of shows how to edit it. There's also a great WordPress Stack Exchange where that may be helpful in the future!

Community
  • 1
  • 1
Howdy_McGee
  • 10,422
  • 29
  • 111
  • 186
  • the code works (except for a missing quote after after $caption) but it still won't return the caption... any other suggestions? Thanks!! – user3540380 Apr 18 '14 at 17:29
  • What do you see when you `echo print_r(get_post(get_post_thumbnail_id($gallery['id'][$i])));` ? – Howdy_McGee Apr 18 '14 at 17:43
  • 'WP_Post Object ( [ID] => 179 [post_author] => 1 [post_date] => 2014-04-18 13:49:38 [post_date_gmt] => 2014-04-18 13:49:38 [post_content] => [post_title] => _MG_7936 [post_excerpt] => [post_status] => inherit [comment_status] => open [ping_status] => open [post_password] => [post_name] => _mg_7936 [to_ping] => [pinged] => [post_modified] => 2014-04-18 13:49:38 [post_modified_gmt] => 2014-04-18 13:49:38 [post_content_filtered] => [post_parent] => 105 [guid] => http://creativewp/wp-content/uploads/2014/04/MG_7936.jpg [menu_order] ... – user3540380 Apr 18 '14 at 17:47
  • I get this for every image in the gallery – user3540380 Apr 18 '14 at 17:48
  • You get the same output, or different output with the same result? `post_contnet` will be the description field and excerpt will be the caption. Make sure each image has a caption to verify that it works. – Howdy_McGee Apr 18 '14 at 18:07
  • same output for each image... `post_content` and `post_excerpt` return nothing but if I use `post_title` i get all the images with the same title... thank you again for your help! – user3540380 Apr 18 '14 at 18:14
  • Whoops, in the `get_post_thumbnail` function, change `$gallery['id'][$i]` to this :: $gallery[$i]['id'] – Howdy_McGee Apr 18 '14 at 18:17
  • It seems to output the same results... could it be that we are getting the post for the whole gallery rather than the individual image? (that was a shot in the dark!) – user3540380 Apr 18 '14 at 18:27
  • Still no luck... print_r outputs a '1' for each image – user3540380 Apr 18 '14 at 20:03