A guide to Get the Image

Get the Image is a plugin that grabs images for you. It was designed to make the process of things such as adding thumbnails, feature images, and/or other images to your blog much easier, but it's so much more than that. It is an image-based representation of your WordPress posts.

To use this plugin, you must be familiar with two very important things in WordPress: the Template Hierarchy and The Loop. If you don't have a working knowledge of those two things, then you should not attempt to use this plugin. More importantly, you should be familiar with how all of this works within your theme because not all themes are the same.

What this plugin does

This plugin was made to easily find images and add them on pages where the full post isn't shown. This is the order in which the plugin attempts to grab an image.

  1. Looks for an image by custom field.
  2. If no image is added by custom field, check for a post image (WordPresss 2.9+ feature).
  3. If no image is found, it grabs an image attached to your post.
  4. If no image is attached, it can extract an image from your post content (off by default).
  5. If no image is found at this point, it will default to an image you set (not set by default).

How to install the plugin

  1. Uzip the get-the-image.zip folder.
  2. Upload the get-the-image folder to your /wp-content/plugins directory.
  3. In your WordPress dashboard, head over to the Plugins section.
  4. Activate Get The Image.

If you are upgrading from a version prior to 0.3 just overwrite your old files. You'll need to change the calls to the plugin in your template files because how the plugin works has changed. Follow the instructions below to see how the image script should be called.

How to use the plugin

To call the image script, you'll need to use what's called function-style parameters.

Example with function-style parameters

<?php get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => 'thumbnail' ) ); ?>

The image script parameters

By simply making a function call to <?php get_the_image(); ?> within a template file, the script will default to this:

$defaults = array(
	'custom_key' => array( 'Thumbnail', 'thumbnail' ),
	'attachment' => true,
	'default_size' => 'thumbnail',
	'the_post_thumbnail' => true,
	'default_image' => false,
	'order_of_image' => 1,
	'link_to_post' => true,
	'image_class' => false,
	'image_scan' => false,
	'width' => false,
	'height' => false,
	'echo' => true
);
custom_key
This parameter refers to a custom field key (or keys) that you use. Remember, custom field keys are case-sensitive (defaults are Thumbnail and thumbnail).
attachment
The script will look for images attached to the post (set to true by default).
default_size
This refers to the default size of an attached image. You can choose between thumbnail, medium, large (WP 2.7+), or full (the default is thumbnail).
the_post_thumbnail
This refers to the WordPress 2.9's new the_post_thumbnail() feature. By having this set to true, you may select an image from the post image meta box while in the post editor.
default_image
Will take the input of an image URL and use it if no other images are found (no default set).
order_of_image
You can choose for the script to grab something other than the first attached image. This only refers to image attachments.
link_to_post
Whether the attachment image should be linked to the post (set to true by default).
image_class
You can give an additional class to the image for use in your CSS.
image_scan
If set to true, the script will search within your post for an image that's been added.
width
Set the width of the image on output.
height
Set the height of the image on output.
echo
If set to true, the image is shown on the page. If set to false, the image will be returned to use in your own function. (Set to true by default.)

Some examples of how to use this plugin

Example 1: Let's suppose that you want to add thumbnails to your category archive pages. What you'll need to do is open your category.php file and add this code within the Loop:

<?php get_the_image(); ?>

By default, that will look for an image with the custom field key Thumbnail and thumnbail. If that image doesn't exist, it will check if a post image has been set. If that image doesn't exist, it will search for any images attached to your post.

Example 2: Let's suppose you want a full-sized image and maybe you want to grab it by a custom field key of Feature. Depending on your theme, this will need to go within the Loop in whatever file is calling the featured article.

<?php get_the_image( array( 'custom_key' => array( 'Feature' ), 'default_size' => 'full' ) ); ?>

If no feature image exists by custom field, it will look for images attached to your post.

Example 3: If you want to have a sort of fallback image, then you can set an image for the script to default to if no other images are found.

<?php get_the_image( array( 'default_image' => 'http://mysite.com/wp-content/uploads/example.jpg' ) ); ?>

Example 4: You can even make the script scan for images that have been added to your post with this:

<?php get_the_image( array( 'image_scan' => true ) ); ?>

Example 5: You might want to make the script grab the second attached image to a post. You can do that with this code:

<?php get_the_image( array( 'order_of_image' => 2 ) ); ?>

A real-world example

This is an example Loop, which may differ slightly from your theme, but the concept is the same. The call to get the image can go anywhere between the opening and closing lines.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

	<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

		<?php get_the_image( array( 'custom_key' => array( 'feature_img' ), 'default_size' => 'medium', 'width' => '200', 'height' => '200', 'image_class' => 'feature' ) ); ?>

		<h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

		<div class="entry-summary">
			<?the_excerpt(); ?>
		</div>

	</div>

<?php endwhile; endif; ?>

Protect yourself from errors in the future

Sometimes, we stop using plugins, but we forget to remove the function calls to the plugin in our theme files. When deactivated, this causes errors. To protect yourself from these errors, you can call the image script like this:

<?php if ( function_exists( 'get_the_image' ) ) { get_the_image(); } ?>

Basically, this just checks to see if the plugin is activated and has loaded the appropriate function.

Styling your images

The plugin will help you style your images by giving you some CSS classes to work with. It will turn your custom field keys and default size into CSS classes. You can also choose to input your own class.

By default, you can add this to your CSS:

img.thumbnail { }

Let's suppose you've used this code:

<?php get_the_image( array( 'custom_key' => array( 'Donkey Kong', 'mario' ), 'default_size' => 'full' ) ); ?>

This will give you these CSS classes to work with:

img.full { }
img.donkey-kong { }
img.mario { }

You can also input a custom CSS class like so:

<?php get_the_image( array( 'image_class' => 'custom-image' ) ); ?>

You will still have the default_size and custom_key classes plus your additional class:

img.custom-image { }
img.thumbnail { }

Plugin support

I run a WordPress community called Theme Hybrid, which is where I fully support all of my WordPress projects, including plugins. You can sign up for an account to get plugin support for a small yearly fee ($25 USD at the time of writing).

I know. I know. You might not want to pay for support, but just consider it a donation to the project. To continue making cool, GPL-licensed plugins and having the time to support them, I must pay the bills.

Copyright & license

Get the Image is licensed under the GNU General Public License, version 2 (GPL).

This plugin is copyrighted to Justin Tadlock.

2008 – 2009 © Justin Tadlock. All rights reserved.