• Hi,

    I’m running twenty eleven through a child theme.

    What I would like to do is set featured image for a post without it changing the default header.

    I want to do this so that when someone shares a link to that page, a chosen image shows up as the thumbnail rather than the header image.

    For some of my posts, It doesn’t work to put an extra image in the body of the post.

    Is there any way to set a featured image to show up as the thumbnail for re-postings of the page, but not change the header of the page itself?

    Thanks.

Viewing 11 replies - 1 through 11 (of 11 total)
  • In your child theme’s copy of header.php, locate:

    <?php
    /*
     * The header image.
     * Check if this is a post or page, if it has a thumbnail, and if it's a big one
     */
    if ( is_singular() && has_post_thumbnail( $post->ID ) &&
    		( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( $header_image_width, $header_image_width ) ) ) &&
    		$image[1] >= $header_image_width ) :
    	// Houston, we have a new header image!
    	echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
    else :
    	// Compatibility with versions of WordPress prior to 3.4.
    	if ( function_exists( 'get_custom_header' ) ) {
    		$header_image_width  = get_custom_header()->width;
    		$header_image_height = get_custom_header()->height;
    	} else {
    		$header_image_width  = HEADER_IMAGE_WIDTH;
    		$header_image_height = HEADER_IMAGE_HEIGHT;
    	}
    	?>
    <img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
    <?php endif; // end check for featured image or standard header ?>

    and change it to:

    <?php
    	// Compatibility with versions of WordPress prior to 3.4.
    	if ( function_exists( 'get_custom_header' ) ) {
    		$header_image_width  = get_custom_header()->width;
    		$header_image_height = get_custom_header()->height;
    	} else {
    		$header_image_width  = HEADER_IMAGE_WIDTH;
    		$header_image_height = HEADER_IMAGE_HEIGHT;
    	}
    	?>
    <img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
    Thread Starter Explorz

    (@explorz)

    Thanks esmi,

    Will that keep other posts from setting the header with a custom featured image when I want a specific featured image for the header? Or only for posts that I choose a featured image that is smaller than the normal header size?

    That will stop all posts from using the featured image as the header. If you want to stop specific posts from using the featured image as the header on an ad hoc basis, the best approach would probably be to use custom fields and have something like:

    <?php
    /*
     * The header image.
     * Check if this is a post or page, if it has a thumbnail, and if it's a big one
     */
    if ( is_singular() && get_post_meta( $post->ID, 'featured-header', true ) && has_post_thumbnail( $post->ID ) &&
    		( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( $header_image_width, $header_image_width ) ) ) &&
    		$image[1] >= $header_image_width ) :
    	// Houston, we have a new header image!
    	echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
    else :
    	// Compatibility with versions of WordPress prior to 3.4.
    	if ( function_exists( 'get_custom_header' ) ) {
    		$header_image_width  = get_custom_header()->width;
    		$header_image_height = get_custom_header()->height;
    	} else {
    		$header_image_width  = HEADER_IMAGE_WIDTH;
    		$header_image_height = HEADER_IMAGE_HEIGHT;
    	}
    	?>
    <img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
    <?php endif; // end check for featured image or standard header ?>

    This assumes that your custom field is called featured-header. Having said that, we’re not in the Loop at this point. so I’m, just assuming that get_post_meta( $post->ID, 'featured-header', true ) will work.

    Thread Starter Explorz

    (@explorz)

    Unfortunately that coding removed all my custom headers for all other posts for which I created unique headers.

    Did you set up the custom field for all of these posts with custom headers?

    Thread Starter Explorz

    (@explorz)

    Thanks for the updated version.

    I have no idea what the custom field is called. Not sure if it is worth the hunt. Thank you.

    Did you review the pages at the links I posted above?

    Thread Starter Explorz

    (@explorz)

    Did you set up the custom field for all of these posts with custom headers?

    No, it is a part of the twenty eleven theme to replace the header (default header set up in the dashboard) with the featured image. You can set that for each individual post. So, some posts I chose a featured image and chose an image that was the right height and width and that shows up as the header for that post.

    Then, when that post gets shared on Facebook, the custom header shows up as the thumbnail (at least a cropped version of that header).

    Thread Starter Explorz

    (@explorz)

    I don’t see any posted links above?

    Do you mean the php coding? Yes, I tried the first one and it removed all my custom headers. I saw the second one and am not sure if I want to risk breaking something with a guess as to what the custom field is called.

    when that post gets shared on Facebook, the custom header shows up as the thumbnail

    There’s nothing we can do about that. It’s well beyond WordPress’ influence.

    Do you mean the php coding?

    No – the link to the page about custom fields.

    Thread Starter Explorz

    (@explorz)

    To be clear, for the posts that I’ve changed the custom header – I want the custom header to show up as the thumbnail. That’s great.

    It is for the pages that I don’t want a custom header, but I do want a featured image. On some posts I am trying to keep the default header and yet set a featured image to show up as the thumbnail for when the post gets shared. Right now the default header is showing up as the thumbnail.

    I’ll check out the link. I hadn’t seen it before.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Set featured image but don't change header’ is closed to new replies.