WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] passing a variable from Post to Functions.php (2 posts)

  1. Manuel Schmalstieg
    Member
    Posted 4 years ago #

    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 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/

  2. Manuel Schmalstieg
    Member
    Posted 4 years ago #

    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!!

Topic Closed

This topic has been closed to new replies.

About this Topic