Support » Fixing WordPress » Display featured image from a particular page

  • Resolved cokeyblokey

    (@cokeyblokey)


    I have a PAGE (News) set as my POSTS FRONT PAGE. I want the featured image (header image) I have set on this PAGE (News) to be used as the header-image used in the posts template (index.php). This will enable the client to simply change the header-image on the News page. Somehow I need to tell the header.php file to get the featured image from the news PAGE (I presume via it’s id, which is 47).

    Can anyone help me out here with some code?

    This is what I have in my header.php to handle the featured image in pages:

    <?php
    // Check if this is a post or page, if it has a thumbnail, and if it's a big one
    if ( is_singular() && current_theme_supports( 'post-thumbnails' ) &&
    has_post_thumbnail( $post->ID ) &&
    ( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail' ) ) &&
    $image[1] >= HEADER_IMAGE_WIDTH ) :
    // Houston, we have a new header image!
    echo get_the_post_thumbnail( $post->ID );
    elseif ( get_header_image() ) : ?>
    <img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" class="header-img" alt="" />
Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter cokeyblokey

    (@cokeyblokey)

    I realise the above isn’t too easy to understand..

    …What I’m trying to achieve is to use the featured image from one page (id=47 – it’s the page used as my static post page) as the header image for my posts.

    I need it working this way as I want the client to be able to change the header image when they want. The single posts are using featured images but not as a header. I need the header to come from the featured image used on page 47.

    Are you using a child theme?

    Thread Starter cokeyblokey

    (@cokeyblokey)

    no, I’m using a modified version of HTML5 RESET

    Try:

    <?php
    if ( is_singular() && current_theme_supports( 'post-thumbnails' ) ) :
    echo get_the_post_thumbnail( '47', 'full' );
    elseif ( get_header_image() ) : ?>
    <img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" class="header-img" alt="" />
    <?php endif;?>
    Thread Starter cokeyblokey

    (@cokeyblokey)

    Thanks esmi,

    That’s working great for my single posts but still no header image being displayed on my Static Posts page.

    Static posts? Can you post a link, please?

    Thread Starter cokeyblokey

    (@cokeyblokey)

    By static posts page, I mean the page set in ‘Settings/Reading/’ as the static front page for posts. Based on the code you supplied, I’ve now used this and it works:

    <?php
    if ( is_singular() && current_theme_supports( 'post-thumbnails' ) ) :
    echo get_the_post_thumbnail( '47', 'full' );
    elseif ( is_home() && current_theme_supports( 'post-thumbnails' ) ) :
    echo get_the_post_thumbnail( '47', 'full' ) ?>
    <img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" class="header-img" alt="" />
    <?php endif;?>

    Can that be shortened?

    Try:

    <?php
    if ( ( is_singular() || is_home() ) && current_theme_supports( 'post-thumbnails' ) ) :
    echo get_the_post_thumbnail( '47', 'full' );
    <img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" class="header-img" alt="" />
    <?php endif;?>
    Thread Starter cokeyblokey

    (@cokeyblokey)

    Great thanks esme!!

    was missing a ?> at the end of the if statement, so here’s working code:

    <?php
    if ( ( is_singular() || is_home() ) && current_theme_supports( 'post-thumbnails' ) ) :
    echo get_the_post_thumbnail( '47', 'full' ); ?>
    <img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" class="header-img" alt="" />
    <?php endif;?>

    Thanks so much esmi – you’ve been a true star!
    Marc

    Glad I could help 🙂

    this was an awesome tip Esmi! thanks so much. I was having a heck of time getting my featured image to appear on top in my sidebar area, but it was the order placement of my code that made the difference. I added your code to a template-part file and called it from my home page template.

    Thanks again, Bob

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Display featured image from a particular page’ is closed to new replies.