Title: Post Thumb Script
Last modified: August 19, 2016

---

# Post Thumb Script

 *  Resolved [Mike](https://wordpress.org/support/users/belinep/)
 * (@belinep)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/post-thumb-script/)
 * Establishing info. I have Exec-PHP (to execute PHP in Text widgets), and a custom
   field “Image” for _certain_ posts.
 * I am trying to get this code to work:
 *     ```
       <?php query_posts('cat=5,10'); ?>
   
       <?php while ($pt_count=>3) : the_post(); ?>
   
       <?php if get_post_custom_values("Image") { ?>
   
       <div class="thumbnails" style="text-align=center;">
   
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
       <img src="<?php $values = get_post_custom_values("Image"); echo $values[0]; ?>" alt="" />
   
       </a></div>
   
       <?php ; $pt_count++ } endwhile; ?>
       ```
   
 * The idea is to display 3 thumbnails from the selected categories. I would have
   passed ‘showposts=3’ but if there isn’t an image for the post, I don’t want the
   code to be written, and I want to be sure that 3 display.
 * Any ideas?

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/post-thumb-script/#post-700071)
 * You didn’t say what the problem is. But just looking at your code, I would suggest
   these revisions to make it “prettier”. By that, I mean two things: Easier to 
   read, and less likely to cause parse errors:
 *     ```
       <?php 
   
       query_posts('cat=5,10');
   
       while ($pt_count<=3) :
   
        the_post();
   
        if ( get_post_custom_values("Image") ) { ?>
   
         <div class="thumbnails" style="text-align=center;">
           <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
             <img src="<?php $values = get_post_custom_values("Image"); echo $values[0]; ?>" alt="" />
           </a>
         </div>
   
         <?php
         $pt_count++;
        }
       endwhile;
       ?>
       ```
   
 * **Edit**: After prettifying, I noticed some errors in your code. Try it the way
   it looks now.
 *  Thread Starter [Mike](https://wordpress.org/support/users/belinep/)
 * (@belinep)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/post-thumb-script/#post-700073)
 * Something is happening, but it’s not good (nothing was happening before).
 * The page loads most of the way through, then binds up for about a minute, then
   when id does fully load, the page ends at the end of this loop…
 * EDIT: It breaks all my pages…
 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/post-thumb-script/#post-700074)
 * If the page freezes up, then it sounds like that `while` loop is never resolving.
   Something like this might be a more robust approach, since it won’t cause an 
   infinite loop:
 *     ```
       <?php 
   
       query_posts('cat=5,10');
   
       $pt_count = 0; // initialize counter
   
       if (have_posts()) : while(have_posts()) : the_post();
   
        if ( !($values = get_post_custom_values("Image") ) )
          continue; // go to next post
        // note that we defined $values already
   
        // display the image:
        ?>
         <div class="thumbnails" style="text-align=center;">
           <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
             <img src="<?php echo $values[0]; ?>" alt="" />
           </a>
         </div>
        <?php
        $pt_count++;
   
        if (3 == $pt_count)
         break; // stop looping at 3 images
   
       endwhile;
       endif;
       ?>
       ```
   
 *  Thread Starter [Mike](https://wordpress.org/support/users/belinep/)
 * (@belinep)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/post-thumb-script/#post-700078)
 * OMG! That couldn’t be more perfect!
 * Thank you OH so much!
 * -MiKe-
 *  [Jeremy](https://wordpress.org/support/users/boilr/)
 * (@boilr)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/post-thumb-script/#post-700414)
 * this is almost exactly what im looking for. i want to display the 10 more recent
   posts and to include the custom field “thumb” before each post title link in 
   the sidebar.
 * ive tried implementing the above, and it does pull the image files, but i cannot
   figure out how to make the “category” = the most recent posts?
 * any help would be greatly appreciated!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Post Thumb Script’ is closed to new replies.

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [showposts](https://wordpress.org/support/topic-tag/showposts/)
 * [thumbnails](https://wordpress.org/support/topic-tag/thumbnails/)

 * 5 replies
 * 3 participants
 * Last reply from: [Jeremy](https://wordpress.org/support/users/boilr/)
 * Last activity: [17 years, 6 months ago](https://wordpress.org/support/topic/post-thumb-script/#post-700414)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
