Get Link to Featured Image for use in View

In functions.php create function and shortcode

/** ***************************************************
* get image src for featured image of post (large version)
/* Register our short code at the appropriate time during the WordPress Boot process. */
add_action( 'init', 'wpk_register_shortcodes');

function wpk_register_shortcodes(){
add_shortcode('my_get_thumbsrc', 'my_get_thumbsrc');
/* Function to return the source url for the featured image of a post */
function my_get_thumbsrc($id) {
global $post;
// Here we check if an ID was specified, if not, we use the ID of the current post
$id = ($id) ? $id : $post->ID;
if ( has_post_thumbnail($id)) {
//We're getting the url of the large version of the image here, options are 'thumbnail', 'medium', 'large' or 'full'
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'large');
// $image_url is an array; It holds url, width and height. See wp_get_attachment_image_src at the WP codex
return $image_url[0]; // return just the url

This shortcode can be used in a View to give link to the image file:

[wpv-post-featured-image size="medium"]

Add modal viewer to link:

[wpv-post-featured-image size="medium"]

Original post

Leave a Comment