voceconnect / multi-post-thumbnails

Adds multiple post thumbnails to a post type. If you've ever wanted more than one Featured Image on a post, this plugin is for you.
143 stars 63 forks source link

Getting image caption #15

Closed cdwharton closed 10 years ago

cdwharton commented 11 years ago

Hi, I love this plugin and use it all the time :)

I do have an issue though, I can't seem to retrieve the image caption - I've found this online but it doesn't seem to work. I'm sure there is a simple solution though. If there is it would be good if you could update your docs with it too :)

$second_thumbnail_id = get_post_meta(get_the_ID(), "page_secondary-   image_thumbnail_id", false);
$second_id = $second_thumbnail_id[0];

// The thumbnail caption:
echo get_post($second_id)->post_excerpt;
chrisscott commented 11 years ago

@weare2ndfloor if you are wanting it output in the image tag, you can use the filter noted in http://wordpress.org/support/topic/caption-3

Otherwise, if you need it separately, something like this should work (assuming you are in a loop of the post w/the thumbnails):

$thumb_post_id = get_post_thumbnail_id('post', 'secondary-thumbnail', get_the_ID());
$thumb_post = get_post($thumb_post_id);
$caption = trim(strip_tags( $thumb_post->post_excerpt ));

Obviously, you'd want some error handling around getting the thumbnail ID and thumbnail post.

cdwharton commented 11 years ago

Hi Chris, this doesn't seem to work :(

laurentperroteau commented 10 years ago

I found a solution to get "alt", "caption", "description" and "title"using 2 functions from this post:

function get_attachment_id_from_src ($image_src) {
    global $wpdb;
    $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
    $id = $wpdb->get_var($query);
    return $id;
}

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
    );
}

Get URL of your image first, then attachment ID and then what you want:

$imgPath = MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'secondary-thumbnail');
$imgIdAttachment = get_attachment_id_from_src( $imgPath );
$metaImgDescription = $metaImg['description']; 
chrisscott commented 10 years ago

This can be more simply/cleanly/best-practicingly done w/the code noted on this support thread.

laurentperroteau commented 10 years ago

Mea culpa : finally the chrisscott solution work for me