Support » Themes and Templates » Making post images show up automatically in a theme

  • If you’re interested in building a theme that use images automatically in the posts, you might find this code snippet useful. This will allow a thumbnail image to show up automatically without require the writer to insert any code in the post:

    <?php # get the thumbnail image if one exists
      $image = "";
      $post_attachments = $wpdb->get_results("SELECT guid "
                                      ."FROM $wpdb->posts "
                                      ."WHERE post_parent = '$post->ID' "
                                      ."AND post_type = 'attachment' "
                                      ."ORDER BY <code>post_date</code> ASC "
                                      ."LIMIT 0,1"
                                      );
    
      if ($post_attachments) {
        $image = $post_attachments[0]->guid;
        $image = str_replace(".jpg","-64x64.jpg",$image);
        $image = str_replace(".png","-64x64.png",$image);
        $image = str_replace(".gif","-64x64.gif",$image);
      }
    ?>
    
    <?php if ( $image != "" ) { ?>
        # Do something with the $image
    <? } ?>

    I’ve posted a few more details in case you’re interested here:

    Automatically display images in WordPress themes

  • The topic ‘Making post images show up automatically in a theme’ is closed to new replies.