<?php if (have_posts()) : ?>
    <ul id="thumbnails">
        <?php 
        
while (have_posts()) : the_post();
            
            
$thumb_file "thumb-$post->post_name.png";
            
$upload_dir wp_upload_dir();
            
            
// First check if the thumbnail exists in wp-content directory. It should be named "thumb_<post_slug>.png".
            // To find your post slug, see see WP-admin --> Manage --> Posts --> your post)
            
if (file_exists(ABSPATH."wp-content/$thumb_file"))
                
$thumbnail get_option("home")."/wp-content/$thumb_file";
            
            
// Then look in the upload folder, but only if the "Organize my uploads into month- and year-based folders" 
            // option is disabled, since the upload folder will be dynamic based on current month
            
else if (get_option('uploads_use_yearmonth_folders') != && file_exists($upload_dir['path']."/$thumb_file"))
                
$thumbnail $upload_dir['url']."/$thumb_file";

            
// Finally query the database for attached images with title "thumbnail" or 
            
else {
                
$query "SELECT guid 
                          FROM wp_posts 
                          WHERE (post_status = 'attachment' OR post_status = 'inherit')  
                            AND post_parent = $post->ID
                            AND (post_title = 'thumbnail' OR post_title = '$thumb_file')
                          LIMIT 1"
;

                
// If no thumbnail was found, use default image located in themedir/images
                
if (!$thumbnail $wpdb->get_var($query)) 
                  
$thumbnail get_template_directory_uri()."/images/thumb-default.png";
            }
            
            
?>
            <li>
                <div class="content">
                    <a href="<?php the_permalink() ?>" rel="bookmark" 
                        title="Permanent link to <?php the_title(); ?>" class="thumbnail"><img 
                        src="<?php echo $thumbnail ?>" alt="<?php $post->post_name ?>" /></a>
                    <h3><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h3>
                    <div class="text"><?php the_excerpt() ?></div>
                </div>
                <div class="metadata">
                    <h3>Posted</h3>
                    <p><?php the_date() ?><?php edit_post_link('[e]',' ',''); ?></p>
            
                    <h3>Tags</h3>
                    <p><?php the_category(', '?></p>
                    <p><?php if (get_comments_number($id) > 0comments_number('No comments &#187;''1 comment''% comments' ); ?></p>
                </div>
                <p class="clear"></p>
            </li>
        <?php endwhile; ?>
    </ul>
<?php else : ?>
    <?php if (is_search()) : ?>
        No search results matched your search.
    <?php else : ?>
        Not found
    <?php endif; ?>
<?php 
endif; ?>