Display image from custom field of WordPress post

I’m using the following code to pull random post data onto the sidebar of my blog. How would I add an image pulled from that post’s custom field “athletethumbnail”?
:

<?php
global $wp_query;
$current_id = $wp_query->get_queried_object_id();

    $my_query = new WP_Query( array(
        'post_type'=>'athletes',
        'posts_per_page'=>'1',
        'category' => '36' ,
        'orderby' =>'rand'
        ));

        if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post();

?>
<?php the_title(); ?>

<?php
endwhile;
}

wp_reset_query();
?>

Related posts

Leave a Reply

2 comments

  1. From the docs found here, Under PostMeta Functions

    These functions are intended for use inside The Loop, and all return arrays.
    
    get_post_custom()
    Get all key/value data for the current post.
    ................
    

    Solution : You should use get_post_custom().

    try this :

    $custom = get_post_custom(the_ID());
    echo $athletethumbnail = $custom["athletethumbnail"][0];
    

    note: You should also be able to get away without passing the POST ID, as get_post_custom calls get_the_id id post id is not passed. source here

    After changes :

        <?php
        global $wp_query;
        $current_id = $wp_query->get_queried_object_id();
    
            $my_query = new WP_Query( array(
                'post_type'=>'athletes',
                'posts_per_page'=>'1',
                'category' => '36' ,
                'orderby' =>'rand'
                ));
    
                // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    <?php 
     $custom = get_post_custom(the_ID());
     echo $athletethumbnail = $custom["athletethumbnail"][0];
     the_title(); ?>
    
    <?php
          endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>
    
    1. Log in to your Facebook account.

    2. Enter “Facebook Developers” into the search box on your profile page. Click the “Facebook.Developers.com” link.

    3. Click the “My Apps” link on the Facebook Developers menu bar. You may have to click “Allow” to proceed.

    4. Click the “Set Up New App” button in the upper right-hand corner of the “Developer” page.

    5. Enter the name of your new Facebook application in the “App Name” field. Click the “Agree” radio-button option to accept the “Facebook Terms,” then click “Create App

    6. Type the text displayed in the “Security Check” box — exactly as it appears on the screen — in the “Text in the box” field. Click the “Submit” button.

    7. Enter a description of your new application in the “Description” field. Select the default language for the app in “Language” drop-down box. Select either the “Email” or the “URL” option for your “User Support Address.” This is the address users use to contact you about problems or support issues with your Facebook app.

    8. Enter your user support URL address or email address. Enter the URL address for your Privacy Policy page. All Facebook app developers must display a Privacy Policy on their websites that explains what type of Facebook user information the app collects and uses. If you require users to accept “Terms of Service” before using your application, enter the URL address containing the agreement text in the “Terms of Service URL” field.

    9. Click the “Save Changes” button. Facebook saves the information changes for your new app and displays summary information on the next page. The summary page displays the Facebook Application ID for the new app under the “App ID” header. You can then start writing code for your new Facebook application.