Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show featured image on 'Posts Page' in Wordpress

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)

Let me know if anything is not clear.

like image 617
Quiver Avatar asked Aug 31 '15 19:08

Quiver


1 Answers

After spending the past two hours researching and trying different things I was able to figure out a solution.

<?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>
like image 114
Quiver Avatar answered Oct 12 '22 17:10

Quiver