Home > Enterprise >  How to get image title and description from Wordpress Media
How to get image title and description from Wordpress Media

Time:11-04

I have a product page where you can create your own product and with each choice you make the image that is shown changes.

Once the user clics on a button I want to use a shortcode to get the title and the description, of the currently shown image, from the Media section of Wordpress.

Now I have found a lot of posts with this code (that you have to insert in functions.php)

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

And then the function was called with this code

$attachment_meta = wp_get_attachment($attachment_id);

Edit: I found the code on this post here

But the data gathered by this is only about the product page (so the title and the description of the page) and it's not what I'm looking for.

Also the only way I found to get the attachment_id of an image on the page is with this article here But it does so manually and I need to get it done in the shortcode...

Here's the whole code

add_shortcode( 'afficher_produits_devis2', function(){
    $attachment_meta = wp_get_attachment($attachment_id);
    echo $attachment_meta['caption'];
} );

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

EDIT 2

I may have found a solution for this conondrum with this post from @Ruvee but now I have to find a way to parse the html of the page and get the URL of the image.

EDIT 3

I found out why I could only get the thumbnail data, turns out the shortcode activated directly when I loaded the product page and not when I clicked the button. So it only activated once and it always got the first image to appear (the thumbnail one).

I corrected the mistake and now it works as I wanted Thanks @Ruvee

CodePudding user response:

"I have to find a way to parse the html of the page and get the URL of the image."

No that's not necessary! Like I said in this answer second way of grabbing the metadata without the actual image url is to use id of the image. You could get id of the image by using get_post_thumbnail_id function.

So your shortcode would be something like this:

add_shortcode( 'afficher_produits_devis2', function(){
    global $post;
    $image_id = get_post_thumbnail_id($post->ID);
    $image_caption = get_post_field('post_excerpt', $image_id);
    echo $image_caption;
} );
  • Related