I'm using the_post_thumbnail(); (aka featured image) to show a news source logo. If I use that image, I'd like to output the name of the news source. For example:

if the_post_thumbnail is "new-york-times.jpg" echo "New York Times" elseif the_post_thumbnail is "cbs-news.jpg" echo "CBS News" etc...

Any help writing this code is greatly appreciated!


If You need title from media use something like this:

$thumb = get_post(get_post_thumbnail_id());
echo $thumb->post_title;

From a quick look at the Codex and some of the source code, I'm not seeing any built-in function that would easily/cleanly get you the file name. I've not played around with the post thumbnail functions much, but if you can figure out in advance the IDs are for each of your thumbnails, then you could try using get_post_thumbnail_id? Something like this pseudo-code perhaps:

    $thumb = get_post_thumbnail_id();
    switch ($thumb) {
        case "1":
            $source = 'CBS';
            return $source;
        case "2":
            $source = 'New York Times';
            return $source;

And then <?php echo $source ?> where you want that bit of text to display?

  • This might be simpler than I thought. Is it possible to output the title of the_post_thumbnail (from the Media Library)? I couldn't find anything in the Codex/forums. – Ryan Mar 8 '11 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.