How to make Get The Image only use embedded images

If you’re a Theme Developer you probably heard of Justin Tadlocks incredible plugin already – for those of you who don’t, let me introduce Get The Image to you.

It’s THE way to extract images from your posts and insert them any way you like in your custom made theme. It does this by checking (in this order)

  • if you have a featured image inserted, or any other custom field defined
  • if not, it’ll see if one is attached to the post
  • if not then it will scan your content for an image embed
  • and if that fails you can display a default image you select

Get The Image will also provide a link to the post when you display the image, or alternatively give you the URL if you want to use it some other way.

I needed an application that displayed only an image that’s embedded in the post.

The Problem: Display the first picture in a post, even if you’ve uploaded it last

Say you write a post, then add a picture to it and review it. Next thing you know is that another picture fits the bill better, so you add that to the post as well. You keep the first one, but show it towards the end of your post. So your first shown picture is technically the last one you’ve uploaded.

By default Get The Image would still display your first uploaded picture, even though that doesn’t represent your post anymore. Let’s see if we can change this to our advantage.

Calling Get The Image

First of all you need to call Get The Image with the scan_image parameter for this feature to become active. You do this like so:

get_the_image( array('image_scan' => true));

Or, if you’d like to call other parameters, you’d add them like this:

get_the_image( array('size' => 'thumbnail', width => '200', 'image_scan' => true, 'order_of_image' => 1));

And really, you should be calling it with an “if function exists” statement in case the plugin isn’t activated:

if ( function_exists( 'get_the_image' ) ) get_the_image( array('size' => 'thumbnail', width => '200', 'image_scan' => true, 'order_of_image' => 1));

Also, if you’re calling all this from within HTML then it should be wrapped in PHP tags like this:

<?php if ( function_exists( 'get_the_image' ) ) 
get_the_image( array('size' => 'thumbnail', width => '200', 'image_scan' => true, 'order_of_image' => 1)); ?>

You get the picture.

But how to we make it ONLY show scanned images?

Even though the feature is now active, Get The Image still looks for a custom field or an attached image by default, and it will always show the one you define with the order_of_image parameter. It defaults to the first picture, but you could set it to any picture you like.

To switch the first two options and make it look for an embedded image straight away (and only) just pass these two parameters:

'attachment' => false, 'the_post_thumbnail' => false

So your compete statement would look like this:

<?php if ( function_exists( 'get_the_image' ) ) get_the_image( array('attachment' => false, 'the_post_thumbnail' => false, 'image_scan' => true, 'order_of_image' => 1)); ?>

Hope this helps!





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.