I'm trying to create a function that checks for the existence of a post_thumbnail()
for a post in Wordpress
and sets an element's background image appropriately. Here's what I've tried:
<div class="header-cover-image" style="background-image: url(
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { echo get_template_directory_uri() . '/assets/img/article-overlay-1.jpg' }?>
">
</div>
This currently crashes/causes the page to render nothing at all. I'm so confused about when and where to use quotation marks when using PHP within HTML markup!
Any ideas how I can make this work?
There was a problem with trying to echo the Wordpress function and append the string simultaneously. Fixed it by doing:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { $uri = get_template_directory_uri(); echo $uri . '/assets/img/article-overlay-1.jpg'; }?>
">
Try bloginfo ;
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { echo get_bloginfo('stylesheet_directory') . '/assets/img/article-overlay-1.jpg'; }?>
Cheers!