I want to be able to echo the URL of the featured image of a post and after looking some on the web I found the following which works fine when I put it in a loop in my front page template.

<?php $thumbSmall = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'small' );
$urlSmall = $thumbSmall['0']; ?>

<?php echo $urlSmall; ?>

However, want to use variable $urlSmall in other places than in the front page template, and this is where my limited coding skills let me down. I tried to just copy paste

wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'small' );
$urlSmall = $thumbSmall['0'];

into my functions.php but that did not work. I need these variables to be globally recognized. What do I do here? write some kind of function?

2 Answers
2

You can turn your snippet into a function that returns the post thumbnail URL of a post:

function wpse81577_get_small_thumb_url( $post_id ) {
    $thumbSmall = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'small' );
    return $thumbSmall['0'];
}

Usage, supplying the ID of a post:

<?php echo wpse81577_get_small_thumb_url( 59 ); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *