Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
Do you mean post thumbnails? Or are you looking for something different? –  m0r7if3r Jan 31 '12 at 14:09
    
Did my answer solve your problem? :) –  Ole Henrik Skogstrøm Feb 1 '12 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. –  Chip Bennett Feb 15 '12 at 14:44
add comment

1 Answer

up vote 4 down vote accepted

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.)

share|improve this answer
1  
Hope you don't mind the edits. Upvoted your asnswer. :) –  Chip Bennett Jan 31 '12 at 15:25
    
Nice edit, now i learned something too ;) thank you! –  Ole Henrik Skogstrøm Jan 31 '12 at 23:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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