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:
<?php
// 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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.