Post Pic

JPWeb asked “How to get thumbnails from specific posts?”

Nowadays, many WordPress users are using a custom field to illustrate their posts with a thumbnails, just like I do on that blog. One of our readers, JPWeb, asked how to get the thumbnails related to a specific kind of posts, most commented, for exemple. Let’s solve JPWeb’s problem.

To get a list of thumbnails from the most commented post, we have to use a SQL request and some PHP. Paste the following code anywhere on your theme, where you'd like the thumbnails to be displayed. Each thumbnail contains a link to read the associated post.

<ul>
<?php

//The name of custom field you'd like to get, don't forget to change it
$custom_field_name = "featuredimg";

//Cool sql request
$sql = "SELECT p.comment_count, p.ID, m.post_id, m.meta_key, m.meta_value
FROM wp_posts p, wp_postmeta m
WHERE p.ID = m.post_id
LIMIT 0,10;";

$result = $wpdb->get_results($sql);

foreach ($result as $topten) {
    $postid = $topten->ID;
    $title = $topten->post_title;
    $commentcount = $topten->comment_count;
    if ( $topten->meta_key == $custom_field_name) {
    $img = '<img src="'.$topten->meta_value.'" alt=""/>';
    ?>
         <li><a href="<?php echo get_permalink($postid); ?>"><?php echo $img; ?></a></li>
    <?php }
}
?>
</ul>

Once you saved the file, this will work perfectly. Notice that this code get thumbnails from the ten most commented posts, but you can easily modify the number of thumbnails to get: Edit the 0,10 in the sql query, replacing it by 0,3 to get images from the 3 most commented posts.

10 Responses

Jan 16 2009 11:59

Hi JB, I was looking for something like that ! How could I use it in my sidebar (via widget for example ?). Thanks.

Jan 16 2009 13:44

You can use it in your sidebar if you paste it on sidebar.php.
To be used as a widget, this code might be turned to a widget first. I’m currently writing a tutorial about widgets, so stay tuned :)

Jan 16 2009 14:22

This one sounds nice.. thanks for the info.. :)

Jan 16 2009 19:58

Merci, Jean-Baptiste !

Jan 17 2009 15:54

Thank you for being so kind to reply my question! You are the greatest! ^_^ / Finally my thumbnails on most commented! : )

I just sent you a $10 donation. Hope you enjoy a drink donated by me ^_^ /

ps.although I’m sorry that i am a newbie and i need to ask this, but what about thumbnails in related posts? I guess is similar but as i have no clue, what else do i have to change to get thumbnails on related posts? Thanks again for your useful recipes!

Jan 17 2009 19:46

@JPWeb: Thanks for your donation! This is really appreciated :)

You mean he related posts widget?

Jan 17 2009 21:57

Hi! Jean, you deserve lots of donations ^_^ / You help a lot to all the newbies like me because for us php sql etc is all greek @_@

Yes, I do have a plugin called related posts but unfortunately the domain of the developer expired, so i cant ask there. It is a single .php file and i see a code very similar similar to the one you gave me to show thumbnails on most commented, so i guess if it is just a matter to add the code for $img -thumbnail- in his output.

I have been trying myself to add the code to call the thumbnail in the output, although with none progress : ( I uploaded it here the plugin http://www.japanf.com/relposts.zip Would you be so kind to take a look at his output ? (// Primary SQL query). Please hope you can give me a hand 0_^ Of course for all your hard work, another drink donated by me (^_-) /

thanks!

JPWeb

ps. I also liked and visited your interesting sponsors (^_- /)

Jan 18 2009 02:06

Great one, here. I was just looking up how to do this when I stumbled over your site. Thanks.

Jan 18 2009 03:55

Very cool, I just did a similar post like this. About pulling posts only containing a certain custom field: Custom fields outside the loop.

I also had someone point out a way to use the query_post to lessen the load on the database. Check it out!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required