postimg
May 2009 11

get-the-image

Breaking out of Blog Themes

We love WordPress at Item-9 Consulting. Although it began as a blogging platform, I would argue that it’s the most versatile, well-supported, user-friendly content management system available today. Need proof?  Here’s a link to 25 beautiful, unique, free WordPress themes, including a section for 1) blogs, 2) galleries (portfolios) and 3) magazines (news).

So you know about blog themes, but what differentiates those from galleries and magazine themes (which have become very popular in the last two years)? Besides alternative layout structures, the non-blog themes rely on prescient images to draw readers in and help them navigate the site. Even though it’s not WordPress, the New York Times website uses the magazine style layout and images perfectly to entice readers to jump to other articles, and subsequently, stay on its site longer (and of course, the longer a reader is on a site, the more likely he will be to click on advertising, thus generating revenue for the site).

 

nytimes

Screw Custom Fields

Alright, enough background info. The reason I’m writing this tutorial is to show you the easiest way to automatically pull images into your WordPress site from your posts. A lot of themes use custom fields to handle this task, but I say, why enter information onto a post more than once? How long would WordPress have lasted if you had to enter the post’s title twice? Probably not long, because that would get annoying quickly. Instead of going the custom field route (and using non-WordPress resizing scripts like timthumb.php), I recommend using the Get The Image WordPress Plugin by Justin Tadlock. A quick list of reasons:

  • You can still specify a custom field to pull—great for backwards compatibility with whichever technique you’re replacing, or if you are just super-picky about which image gets used.
  • If you’re lazy like me, the plugin pulls the first (or second or third, etc.) image you attached to your post.
  • If you’re using an image from another post, you can set Get the Image to scan your post and use whichever image it comes across first.
  • No image at all in the post? Specify a default image to use in these cases so your layout never breaks.
  • You can specify the images’ output size (both/either height and width).
  • You can specify which WordPress resized image you want to work with (examples from 2.7 are thumbnail, medium, large and full size).

Sounds great, huh? It definitely is great, but first, there are a few caveats to note:

  1. All this automatic image pulling needs to be coded into the loop. That means you need to be comfortable with the guts of WordPress and php or extremely patient in making mistakes and asking for help (Justin does provide support for all his plugins for the low price of $25/year—well worth it, IMHO)
  2. The documentation is available but it’s incomplete. That’s inevitable considering the limitless possibilities a user could take with the plugin. Again, invest in the support forums.
  3. You’re not currently going to find this built into many themes (excepting Theme Hybrid from Justin), so you’ll have to do the work yourself.
Zap Props Sandbox Site

Zap Props Sandbox Site

The Code

Time for a practical example. I’m currently working on a new client’s site using the Foliotastic created by ThemeSnack, which I purchased from ThemeForest. The theme has a great design and great support, but I couldn’t get ThemeSnack’s image pulling solution to work. Just like several users on the ThemeForest wrote, I tried everything, but after several hours of frustration I said, “enough” and did what I knew had to be done—throw in the towel and try a new approach. I’ll skip the melodramatics and show you what I began with:

64
65
66
67
68
69
<a href="<? the_permalink() ?>">
    <?php if(get_post_meta($post->ID, "post_image_value", true)) { ?>
        <img src="<?php bloginfo('template_url'); ?>/img_resize/timthumb.php?src=<? echo get_post_meta($post->ID, 'post_image_value', true) ?>&amp;w=620&amp;h=290&amp;zc=1" alt="<? the_title() ?>" />
    <?php } else { ?>
    	<img src="<?php bloginfo('template_url'); ?>/graphics/default_thumb.gif" alt="<? the_title() ?>" /><? } ?>
</a>

And here’s my changes:

64
65
66
<a href="<? the_permalink() ?>">
      <?php get_the_image(array('custom_key' => array('post_image_value'), 'default_size' => 'full', 'default_image' => get_bloginfo( 'template_url' ) . '/graphics/default-image.jpg', 'width' => '620', 'image_scan' => true)); ?>
</a>

and according to Justin’s comment below, the code can be further trimmed to one line:

64
<?php get_the_image(array('custom_key' => array('post_image_value'), 'default_size' => 'full', 'image_scan' => true, 'default_image' => get_bloginfo( 'template_url' ) . '/graphics/default-image.jpg', 'width' => '620')); ?>

So Let’s Break it Down

Here’s what the above code does, in order of arguments:

  1. “get_the_image” calls the plugin
  2. ” ‘custom_key’ => array(‘post_image_value’)” pulls an image with the custom field value of “post_image_value”.
  3. If that the custom field is not found, ” ‘default_size’ => ‘full’ ” finds the first attached image and chooses the originally uploaded size.
  4. If no attached images or custom fields are found, ” ‘image_scan’ => true” will find any images that may have inserted from the media library.
  5. If the plugin can’t find any other images, then ” ‘default_image’ => get_bloginfo( ‘template_url’ ) . ‘/graphics/default-image.jpg’ ” will use an image I uploaded to the theme directory (thanks for this code, Justin;).
  6. And finally, ” ‘width’ => ‘620′ ” resizes the width of the image to 620px. I could also specify a height here, but that would likely stretch the image. Instead, I use CSS to hide the image’s overflow and limit the height to 290px. You may not see the whole image, but it’s at least proportionate and I think it looks cool, anyway

That wraps up this tutorial. If you have any questions about the Get the Image plugin, I ask you leave them in the comments. If it’s too technical a question, I’ll refer you to Justin’s support forums I mentioned above. You may also want to visit his post describing the plugin in more detail.

Related Posts

  1. I’m Speaking at a Chicago WordPress Workshop on 11/14 + Link Dump
  2. WordPress Plugins are About to Get Real
  3. How Freelancers Can Use Retainers to Remove the Guessing From Project Estimates
  4. Glengarry Glenn Ross and Web Marketing
  5. Read the Best WordPress Book Ever!

  1. Great tutorial. And, thanks for spreading the word about my plugin.

    You can actually cut your code from three lines down to one. The <code>get_the_image()</code> function will automatically link to the post (unless turned off, of course).

  2. daniel says:

    Ok I am having a problem with the Get The Image WordPress Plugin. When I try to resize an image , the image is not resized proportionally as is the case with timthumb.php. Is there a way to specify custom sizes that can be later called in the code.

  3. Tschai says:

    I've tried to combine 'Get the Image' plugin with TimbThumb but haven't succeeded in making them workt correctly together.

    Although I feel I'm very close, it seems like get_the_image kinda breaks the timbthum line even if I set the echo to false: I can't get it resized!

    Any suggestions?

  4. Sho says:

    OMG!!!! Thank you so much for this tutorial. I could not find anywhere how to insert the default. THANK YOU SO MUCH.

  5. Victor says:

    How do I do to show a text next to the image that's cropped?

  6. Govind says:

    I liked reading your blog…keep up the good work.

  7. Jason Pelker says:

    I promised you I was going to write this tutorial, and gosh darn it, I did. It's my way of saying thank you for all the help you gave me in getting the plugin to work properly. Like I said, WordPress has the best support community out there because of people like you. Not only that, but your plugin is too good and magazine themes are too prevalent for Get the Image to keep being maligned.

    Btw, congrats on getting listed on http://werockwp.com/ this week. Keep up the excellent work!

  8. Jason Pelker says:

    I recommend specifying only one dimension (i.e. width), choosing the 'full' image size and then using CSS to specify the exact opposite dimension (i.e. height) of the image. Anything that doesn't fit in the CSS stated height will then be cropped.

    If that doesn't work, play around with a variation of those instructions–I'm sure you'll get it:)

  9. Jason Pelker says:

    You may want to get onto Justin's forums. I personally see no need to have both image manipulators going at the same time.

    Again, I suggest adjusting one of the dimensions using Get the Image, then using CSS to crop the other dimension.

Speak Up!