I used WordPress’ get_the_post_thumbnail() function in order to output the current post’s featured image. It was great, until I had to link to the featured image. To do that, I needed the value of the SRC attribute of that IMG tag, i.e., the direct link to the featured image.

Thanks to phpxref, and the WordPress Codex, I managed to write a small useful function just for that. Without further ado, I present to you, the function:

function oy_get_direct_thumbnail_link( $post_id = NULL ) {
	global $id;
	$post_id = ( NULL === $post_id ) ? $id : $post_id;
	$src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full');
	$src = $src[0];
	return $src;
}

The first and only parameter ($post_id) is optional. If used inside the loop, it fetches the current post’s ID by itself.

Search terms:

One Response to Get the Direct Link to a Post’s Featured Image

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting