WordPress.org

Ready to get started?Download WordPress

Forums

Retrieve Featured Image Metadata (Title, Caption, Alt Text, Description) (2 posts)

  1. thescribbler
    Member
    Posted 1 year ago #

    Hi everyone,

    I apologise if it's been asked before - I've tried searching and Googling but can't find the solution. I was wondering how I can retrieve the Featured Image metadata (Title, Caption, Alt Text, Description) so that I can assign them to variables and use the values. Specifically, how could I assign any of these to use as the title attribute for a Featured Image in the example below?

    <?php the_post_thumbnail ( 'large', array('title' => $image_metadata)); ?>

    Massive thanks to anyone who can help!

  2. thescribbler
    Member
    Posted 1 year ago #

    I found the answer, so if anyone else is looking for this, here it is:

    WordPress stores each image as its own post. The title of the image is the title of the post, the caption of the image is the excerpt and the description of the image is the content of the post.

    So you can assign each of these to variables like this (code must be placed inside the loop):

    $title = get_post(get_post_thumbnail_id())->post_title; //The Title

    $caption = get_post(get_post_thumbnail_id())->post_excerpt; //The Caption

    $description = get_post(get_post_thumbnail_id())->post_content; // The Description

    Once again, apologies if this has been done to death already - it took me ages to find the answer.

    I found the solution here:

    How to Display WordPress Post Thumbnails with Captions

Topic Closed

This topic has been closed to new replies.

About this Topic