Title: passing a variable from Post to Functions.php
Last modified: August 19, 2016

---

# passing a variable from Post to Functions.php

 *  Resolved [Manuel Schmalstieg](https://wordpress.org/support/users/targz-1/)
 * (@targz-1)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/passing-a-variable-from-post-to-functionsphp/)
 * I have spent the last few days trying to implement some nice WP customization,
   in order to display thumbnail images grabbed from various posts.
 * What I try to achieve: I want to display a series of thumbnails of related posts,
   in the sidebar of my blog. I am trying to adapt [a smart little script](http://wphackr.com/get-images-attached-to-post/)
   found at wphackr.com… and the only thing that does not work is: passing the post
   ID to the script (in the functions.php).
 * I am using this code – located in my sidebar.php – to get the IDs of two random
   posts from the same category:
 *     ```
       <?php $postslist = get_posts('category_name=portfolio&numberposts=2&orderby=rand');
       foreach ($postslist as $post) :
       setup_postdata($post);  ?>
       <?php the_ID(); ?>
       <?php $trueID = the_ID();
       echo $trueID; //this works!!
       bdw_get_images($trueID); //does not work ?>
       <?php endforeach; ?>
       ```
   
 * This piece of code is echoing the ID’s correctly, so this is working. What does
   not work, is sending the $trueID variable to my functions.php. The function starts
   like that:
 *     ```
       function bdw_get_images($postId){
       $iPostID = $postId
       ```
   
 * but this $postId has no effect whatsoever. The result of the function is that
   is displays the latest thumbnail image, but it doesn’t get the post ID.
 * Obviously I’m doing something wrong, but I haven’t found any clear description
   about _how_ to pass this variable to my function. Here is a preview page of my
   blog, so you can see what I’m trying to do … [http://blog.ms-studio.net/2009/08/no-signal/](http://blog.ms-studio.net/2009/08/no-signal/)

Viewing 1 replies (of 1 total)

 *  Thread Starter [Manuel Schmalstieg](https://wordpress.org/support/users/targz-1/)
 * (@targz-1)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/passing-a-variable-from-post-to-functionsphp/#post-1179107)
 * Ha, writing this message obviously had a therapeutic effect !!
 * I realize that the following **does** work:
    `bdw_get_images($post->ID);`
 * I wrongly assumed that this would give me the current post page, but no, since
   we’re outside of the main Loop, it passes exactly the ID I want….
 * Yippy-Yaeeeh!!

Viewing 1 replies (of 1 total)

The topic ‘passing a variable from Post to Functions.php’ is closed to new replies.

## Tags

 * [function](https://wordpress.org/support/topic-tag/function/)
 * [parameter](https://wordpress.org/support/topic-tag/parameter/)
 * [postid](https://wordpress.org/support/topic-tag/postid/)
 * [thumbnail](https://wordpress.org/support/topic-tag/thumbnail/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * 1 reply
 * 1 participant
 * Last reply from: [Manuel Schmalstieg](https://wordpress.org/support/users/targz-1/)
 * Last activity: [16 years, 10 months ago](https://wordpress.org/support/topic/passing-a-variable-from-post-to-functionsphp/#post-1179107)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
