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();
?>
From the docs found here, Under PostMeta Functions
Solution : You should use get_post_custom().
try this :
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 :