Custom Fields outside of loop
-
Hi
I’m trying to query thumbnail photos put into custom fields and display them outside of the loop. I have a “Most Commented” section and I’d like to bring in each post’s respective thumbnail photo, which is housed in a custom field called “small_photo”. You can see what I’m attempting to do if you click on the “Popular” tab at right here:
http://graphpaperpress.com/dev/
Here is the existing code, which wrongly pulls in the “small_photo” of the latest post:
<!-- POPULAR (MOST COMMENTED POSTS) --> <div class="t2"> <ul class="popular"> <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 3"); foreach ($result as $topten) { $postid = $topten->ID; $title = $topten->post_title; $commentcount = $topten->comment_count; if ($commentcount != 0) { ?> <li class="clear"> <h6><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></h6> <a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"> <img src="<?php $key="small_photo"; echo get_post_meta($post->ID, $key, true); ?>" alt="" class="photo_align_left" /></a> <ul class="small"> <li><a href="<?php echo get_permalink($postid); ?>#respond" title="<?php echo $title ?>"><?php echo $commentcount ?> comments</a></li> <li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">Read →</a></li> </ul> </li> <?php } } ?> </ul> </div>
-
1. Try changing:
foreach ($result as $topten) {to:
foreach ($result as $post) {(Of course, you’ll want to change each instance of
$topten->as well.)2. After your foreach line, slip in:
setup_postdata($post);That works! Thanks Kaf!
Hi there..
I have read it but I have a question:is possible upload an image to post or page and display it outside the loop..?
there is a guide or tutorial..?thank’you!
Andreaendortrails & Kafkaesqui, you guys are GENIUSES!!! 🙂 Thank you so much, this is exactly what I was looking for!! 🙂 Live example on this page:
http://www.kevinandamanda.com/recipes/
Thank you again! 🙂
The topic ‘Custom Fields outside of loop’ is closed to new replies.