Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
This is more of a PHP question then a WordPress question, IMHO –  bungeshea_ Jan 15 '13 at 5:20
    
Meaning that... –  Josef Ulander Jan 15 '13 at 18:49

2 Answers 2

up vote 5 down vote accepted

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 ); ?>
share|improve this answer
    
Cool, but how do I get the post ID in <?php echo wpse81577_get_small_thumb_url( 59 ); ?> dynamically, I want it in the a loop? –  Josef Ulander Jan 15 '13 at 18:39
    
e.g. wpse81577_get_small_thumb_url( $post->ID );, wpse81577_get_small_thumb_url( get_the_ID() );, ... –  diggy Jan 15 '13 at 18:54
    
Cool thanks, it works perfect! –  Josef Ulander Jan 15 '13 at 19:03

Pure PHP question, really.

global $urlSmall;
$urlSmall = $thumbSmall['0'];

If you declare the variable with the global keyword when you initialize it it will be available thereafter. You can imprort it, so to speak, with...

global $urlSmall;
var_dump($urlSmall);

You can do the same thing by assigning key/values directly to the $GLOBALS array.

$GLOBALS['urlSmall'] = $thumbSmall['0'];

That seems to be the most direct answer to the question:

I need these variables to be globally recognized. What do I do here?

There may be better ways to handle the data though.

share|improve this answer
    
Well it seems to work for me though! But s_ha_dum how could I achieve something similar without making global variables –  Josef Ulander Jan 15 '13 at 18:41
    
Sorry my bad I was to quick, it didn't solve my problem. When I follow your suggestions and put global $urlSmall; var_dump($urlSmall); in my front page template it just prints NULL on the page. –  Josef Ulander Jan 15 '13 at 18:48
    
If you want your variables globally accessible you need to make global variables. Another option is to put together a function as suggested in another answer. I don't know why you can't get it to work. It does work. You have to make sure that the file where the variable is defined executes before you try to use the variable, so maybe that is it. –  s_ha_dum Jan 15 '13 at 19:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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