Get WordPress Featured Image Information

I wrote a few days ago about getting the WordPress featured image URL. With this in mind I have also recently wanted to take this further on a couple of sites and get some of the data associated with the featured image. Specifically I wanted to be able to easily obtain the featured images URL, title and also the caption. With a little research and remembering that WordPress attachments are just another WordPress post type here is how I did this.

The fact that attachments (uploaded through the WordPress media library) are WordPress post types, just like posts, pages and links as well as any custom post types that you may create, makes accessing this information easy. It also means there is so much you can do with attachments, more than you may have first realised.

My goal here was to create my own template tags for the things that I wanted to do. My previous explained how this was done for the featured image URL and therefore here I set about doing something similar for the title and the caption. Below is the code to use in your functions.php file for getting the WordPress featured image title:

[code language=”php”]

/**
* Returns the title of the featured image assigned to a post.
*
* @param integer $post_id The post ID to get the featured image title from.
* @return string The featured image title or an empty string if no
* title or post doesn’t have featured image
*/
function wpmark_get_featured_image_title( $post_id = 0 ) {

// if we have no post id, use current post id.
global $post;
if ( 0 === $post_id ) {
$post_id = $post->ID;
}

// get the thumbnail id for this posts featured image.
$featured_image_id = get_post_thumbnail_id( $post_id );

// if we don’t have a featured image.
if ( ” === $featured_image_id ) {
return ”;
}

// return the title of the featured image.
return get_the_title( $featured_image_id );

}

[/code]

Notice that this function uses the get_posts() function which may sound straight, but again remember that attachment are a post type and therefore this function can be used. The code for getting the caption is very much the same and is below, again place in your themes functions.php file.

[code language=”php”]

/**
* Returns the title of the featured image assigned to a post.
*
* @param integer $post_id The post ID to get the featured image title from.
* @return string The featured image title or an empty string if no
* title or post doesn’t have featured image
*/
function wpmark_get_featured_image_title( $post_id = 0 ) {

// if we have no post id, use current post id.
global $post;
if ( 0 === $post_id ) {
$post_id = $post->ID;
}

// get the thumbnail id for this posts featured image.
$featured_image_id = get_post_thumbnail_id( $post_id );

// if we don’t have a featured image.
if ( ” === $featured_image_id ) {
return ”;
}

// return the title of the featured image.
return get_the_title( $featured_image_id );

}

[/code]

These two functions combined with the previous function published are very useful when creating a WordPress site, particularly one that requires showing post attachments in a very specific way.

5 thoughts on “Get WordPress Featured Image Information”

  1. How would you go about if you had multiple featured images, and you needed to pull description from all of them individually?

    Great post btw!

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.