WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display featured image from a particular page (12 posts)

  1. cokeyblokey
    Member
    Posted 1 year ago #

    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="" />
  2. cokeyblokey
    Member
    Posted 1 year ago #

    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.

  3. esmi
    Forum Moderator
    Posted 1 year ago #

    Are you using a child theme?

  4. cokeyblokey
    Member
    Posted 1 year ago #

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

  5. esmi
    Forum Moderator
    Posted 1 year ago #

    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;?>
  6. cokeyblokey
    Member
    Posted 1 year ago #

    Thanks esmi,

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

  7. esmi
    Forum Moderator
    Posted 1 year ago #

    Static posts? Can you post a link, please?

  8. cokeyblokey
    Member
    Posted 1 year ago #

    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?

  9. esmi
    Forum Moderator
    Posted 1 year ago #

    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;?>
  10. cokeyblokey
    Member
    Posted 1 year ago #

    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

  11. esmi
    Forum Moderator
    Posted 1 year ago #

    Glad I could help :-)

  12. rwilki
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags