WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Fields outside of loop (5 posts)

  1. Thad Allender
    Member
    Posted 6 years ago #

    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>
  2. Kafkaesqui

    Posted 6 years ago #

    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);

  3. Thad Allender
    Member
    Posted 6 years ago #

    That works! Thanks Kaf!

  4. andreacutelli
    Member
    Posted 5 years ago #

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

  5. kevinandamanda
    Member
    Posted 5 years ago #

    endortrails & 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! :)

Topic Closed

This topic has been closed to new replies.

About this Topic