Home Forums Front-end Issues If statement not working when field is empty Reply To: If statement not working when field is empty

  • Ah… I think you may have solved it.

    I only have two fields (hero_image and thumbnail_image). These are set to appear on all pages and posts including custom post types.

    I’ve just looked in my header.php file and look at what I found:

    	// Get post ID
    	$post_id = get_queried_object_id();
    	// Hero image
    	$hero = get_field('hero_image', $post_id);
    	$hero_url = wp_get_attachment_url( get_field('hero_image', $post_id), 'hero');
    <?php if ( is_single() || is_archive() ): ?>
    	<header id="masthead" class="site-header">
    <?php else: ?>
    	<header id="masthead" <?php if ($hero) { echo 'class="site-header has-background" style="background:url('.$hero_url.')"'; } else { echo 'class="site-header"'; } ?>>
    <?php endif; ?>

    I’ve renamed $post_id in header.php to $post_id_outside_loop. I’ve then used this variable via single.php as it’s also outside the loop. This has solved the issue.

    For anyone that stumbles across this, see this post for more info.