WordPress.org

Ready to get started?Download WordPress

Forums

Making post images show up automatically in a theme (1 post)

  1. rbredow
    Member
    Posted 5 years ago #

    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:

    http://www.185vfx.com/2008/10/automatically-display-images-in-wordpress-themes/

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 5 years ago by rbredow
  • This topic is not a support question
  • WordPress version: 2.6