Wordpress thumbnail as background image, with a defualt fallback
I'm new to php. Developing a WordPress site.
I'd like to pull in a featured image to use as a hero background image on each post/page. If there isn't a featured image, I'd like to use a default image.
I've found working code to do each of the above individually, but not sure how to combine them.
Featured image as background image:
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<div id="post" class="hero-image" style="background-image: url('<?php echo $thumb['0'];?>')">
</div>
Fallback default image:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( 'full' );
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/img/default-img.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
from Recent Questions - Stack Overflow https://ift.tt/3umoSFL
https://ift.tt/eA8V8J
Comments
Post a Comment