3

I'm wondering how I'd be able to set a custom field within my Wordpress posts. I want to have a field available to add an image via the Post dashboard - that has a max width & max height set, I want the image to always float to the left within the posts (Text wrapping around via the right).

Can anyone recommend a plugin for this, or the PHP snippet that I could include to do so (I could add to single.php or functions.php, right?)

Thanks as always!

3
  • Do you mean post thumbnails? Or are you looking for something different?
    – mor7ifer
    Commented Jan 31, 2012 at 14:09
  • Did my answer solve your problem? :) Commented Feb 1, 2012 at 21:05
  • 1
    Any follow-up on this question, from the OP? WPSE is a Question and Answer site, not a drive-by support forum. If the answer below solved your problem, you should indicate that, by clicking the check mark next to the answer. If it didn't solve your problem, you should add comments indicating why not. Commented Feb 15, 2012 at 14:44

1 Answer 1

4

You should use the post thumbnail feature. Add support for this feature with this line in your functions.php file:

add_theme_support( 'post-thumbnails' );

You can also set a new thumbnail size that will be cropped on new added photos to use this in your theme. Add this code to set a new thumbnail size (also in the functions.php file):

add_image_size( 'large-feature', 500, 300, true );

The code above crops the picture to fit exact into 500x300 pixel. to let the width and heigh to be fluid (with a max width of 500 and a max height of 300) set the true property to false.

Finally add the thumbnail to the theme at the location you want. With this code:

echo get_the_post_thumbnail( $post->ID, 'large-feature' );

That's it :) you enabled and customized a post thumbnail pictured! Hope this works for you :)

Edit

Adding a couple suggestions/best practices:

First, be sure to put the functions.php code inside a callback, hooked in appropriately; e.g.:

function theme_slug_setup_thumbnails() {
    // Enable featured image feature
    add_theme_support( 'post-thumbnails' );
    // Add custom image size for displaying
    // featured image within the post
    add_image_size( 'large-feature', 500, 300, true );

}
add_action( 'after_setup_theme', 'theme_slug_setup_thumbnails' );

Second, wrap the template call in a conditional, and output the fully-formed HTML for the image:

if ( has_post_thumbnail() ) {
    the_post_thumbnail( 'large-feature' );
}

(I would also recommend using a unique slug for the custom image size, such as theme-slug-large-feature', wheretheme-slugis the *slug* of your Theme name. Note that I also usedtheme_slugas a unique prefix for thefunctions.php` callback function.)

2
  • 1
    Hope you don't mind the edits. Upvoted your asnswer. :) Commented Jan 31, 2012 at 15:25
  • Nice edit, now i learned something too ;) thank you! Commented Jan 31, 2012 at 23:42

Your Answer

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

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