0

I'm looking for a plugin that pulls an Image from a Wordpress Posts Gallery (Note NOT the Featured Thumbnail, we have thousands of posts and can't retroactively go back and set a Featured image for each post..)

Also some of the plugins I've tested don't let you specify exact dimensions, the Wordpress Get The Image Plugin let's you pull an image from the gallery like I want then you set the width but it tries to maintain proprotions. So basically I need it to do exactly that except not try and maintain proportions but rather do a Zoom Crop if that makes sense.

1 Answer 1

0

No need for plugins. Some simple code in your theme can do this job.

You can get any size image by passing an array of width and height for the size parameters, but to get a cropped image, you have to predefine it as an premade image size so that the crop gets created properly at upload time.

To do that, in your theme's functions.php file, you need to add something like this:

function themename_setup() {
    add_image_size( 'themename-some-description-of-the-size', 100, 100, true );
}
add_action( 'after_setup_theme', 'themename_setup' );

That's the width and height you want, and the "true" saying you want it to crop to the center of the image.

Now you need code in the Loop to pull either the Featured Image (if there is one) or the First Image from the gallery (if there's no featured image). This code would go into The Loop, so that you're getting it for the current post.

if ( has_post_thumbnail() ) {
        $thumb_id = get_post_thumbnail_id();
} else {
    $attachments = get_children( array(
        'post_parent' => get_the_ID(),
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'order' => 'ASC',
        'orderby' => 'menu_order ID',
        'numberposts' => 1)
    );
    $attachment = array_shift($attachments);
    $thumb_id = $attachment->ID;
}

Finally, you need to display the resulting image:

echo wp_get_attachment_image($thumb_id, 'themename-some-description-of-the-size');

Simple. However, there's one caveat: Your already uploaded images won't have this size image created for them. So WordPress will pick the best fitting image and try to use that. This is not always ideal. Use a plugin like Regenerate Thumbnails after you create the code, and it will go through all your images and have WP regenerate the thumbnails, creating the proper image sizes for all of them.

Also remember when making your theme that this code is more of a guideline than code you should actually use. I wrote it to teach you how to do it yourself and to customize your theme, not to be copy-pasta.

You can find out more about image handling in themes here: http://ottopress.com/2011/photo-gallery-primer/

1
  • Oh that's good to know that you don't have to use a plugin, I'm thinking I could make this a function in the Functions.php file and then re-use it throughout the Archive.php and Category pages, that would be perfect, looks like i'll have to regenerate a ton of images with that nifty plugin, but it'll probably be better on the server in the long run.
    – Jordash
    Jul 26, 2011 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.