How to make Get The Image only use embedded images

- by

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!

If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

4 thoughts on “How to make Get The Image only use embedded images”

  1. A user should never directly edit a plugin file. They’ll just lose those changes on the next update of the plugin.

    Plus, the correct instructions for doing what you’re trying to do are included in the plugin’s “readme.html” file.

  2. Hi Justin,

    May I say – what an honour!! You guys at WordPress are my absolute super heroes 😉

    I love your plugin, and believe me I’ve been through the readme file many times. I can’t get it to use the image scan as the first option. Hacking source code is never ideal, but I didn’t see another way.

    Could you point me into the right direction?

  3. Justin,

    You are so right – the answer was right in front of me all along: set attachment and the_post_thumbnail to false and your plugin only uses image_scan if set to true.

    Thank you for the pointer (and for the plugin) 😉

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.