Support » Developing with WordPress » Custom Fields outside of loop

  • Resolved Thad Allender

    (@endortrails)


    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 &rarr;</a></li>
    </ul>
    
    </li>
    <?php } } ?>
    </ul>
    </div>
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Fields outside of loop’ is closed to new replies.