• Resolved TheAustinG

    (@theausting)


    So for all of the other pages on my wordpress site I am able to display the featured image for the page. However, on the page that all of my posts are displayed the featured image does not display even if it is set.

    Here is the code I am using to display the featured image on all other pages.

    <?php if ( has_post_thumbnail() ): {
        $src = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
    }?>
    
    <div class="featured-image-full-width" style="background-image: url( <?php echo $src; ?> ) !important; height: 400px; background-size: cover; background-repeat: no-repeat; background-position: center;"></div>
    
    <?php endif; ?>

    This does not work on the page that is chosen to display posts though. Keep in mind that I need to display the featured image as a background image so that it is full width of the page and not stretched. (IE and Edge don’t support “object-position” so this is my way of working around that)

    I found this solution: https://wordpress.org/support/topic/posts-page-featured-image?replies=15

    And while it does show the featured image, it automatically puts it inside of an <img> which is not what I want. I was just the URL of the featured image so that I can display it as a background-image: url()

    Let me know if anything is not clear.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter TheAustinG

    (@theausting)

    I managed to find a way to solve this problem. Here is what I did:

    <?php if(is_home()) {
        $page_for_posts = get_option( 'page_for_posts' );
        $img = wp_get_attachment_image_src(get_post_thumbnail_id(get_option('page_for_posts')),'full');
        $featured_image = $img[0];
    }?>
    
    <div class="featured-image-full-width" style="background-image: url( <?php echo $featured_image ?> ) !important; height: 400px; background-size: cover; background-repeat: no-repeat; background-position: center"></div>
    Thread Starter TheAustinG

    (@theausting)

    Correction to the code above. One of the lines is unnecessary.

    New code:

    <?php if(is_home()) {
        $img = wp_get_attachment_image_src(get_post_thumbnail_id(get_option('page_for_posts')),'full');
        $featured_image = $img[0];
    }?>
    
    <div class="featured-image-full-width" style="background-image: url( <?php echo $featured_image ?> ) !important; height: 400px; background-size: cover; background-repeat: no-repeat; background-position: center"></div>

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Posts Page Featured Image’ is closed to new replies.