0

I noticed the website http://gawker.com/ does something cool with images in posts - some posts have a large featured image above the title, others have none and just have images in the content.

My best bet on how to achieve this in WordPress is to add <?php the_post_thumbnail('large'); ?> above the title and make a selectable option to display or not display the_post thumbnail in the post edit screen. It would need to only apply to the post (and not the excerpt)... I'm not sure how I would create that option. I would appreciate if anybody knows how this would be done, can direct me to a somewhat relevant tutorial, or has a better idea of how to achieve this.

1
  • 1
    in twentyten I believe there is an example of this. I believe it examines the featured images width and uses it up there if it exceeds 900 or something.
    – GhostToast
    Jun 4, 2013 at 16:22

1 Answer 1

2

This is modified code from twentyten that does what you're looking for. Put this in header or wherever you want the image to run. See string post-thumbnail -- that is size. Make sure it is defined as 940 x 180 in functions.php

if ( is_singular() && current_theme_supports( 'post-thumbnails' ) &&
        has_post_thumbnail( $post->ID ) &&
        ($image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail' ) ) &&
        $image[1] >= 940 ) :
    // Houston, we have a new header image!
    echo get_the_post_thumbnail( $post->ID );
elseif ( get_header_image() ) : ?>
    <img src="<?php header_image(); ?>" width="940" height="180" alt="" />
<?php endif; ?>

It falls back to header_image if that is set. Feel free to change sizes.

13
  • So here is what I put in my posts snippi.com/s/a5910gm and here is what I put in functions.php snippi.com/s/3vwjw1y - shouldn't this be pulling the image if it's over 940 x 180? I have it setup and I'm getting nothing. Jun 4, 2013 at 16:36
  • modifying answer
    – GhostToast
    Jun 4, 2013 at 16:38
  • For some reason this isn't working in my theme - the images are not being pulled and I'm very curious as to why. This is pulling from the featured image, correct? I wonder if there's an easier way to make a "Display image if larger than" type of code. Jun 4, 2013 at 16:47
  • 1
    Yep. You have thumbnail support enabled? And you have post-thumbnail image size defined as 940x180? Note that all of the conditions in the if statement must be met. Perhaps it is too strict. Try debugging a little, see if you can just echo 'hello world' inside the if statement...work your way from there.
    – GhostToast
    Jun 4, 2013 at 16:49
  • Where does the post-thumbnail image size need to be defined? I've tried to define it in Settings > Media and in functions.php snippi.com/s/brzsxol but it still isn't working :-( Jun 4, 2013 at 16:54

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.