Home > Back-end >  Nested If else statements used in a WordPress template isn't working
Nested If else statements used in a WordPress template isn't working

Time:09-28

I'm not sure what is wrong with the nested conditions below. The else block never seems to get executed even when the image variable is not set(hero image being absent). Please help me find the problem with it.

<?php
if ( is_singular() || is_page() ):
if( have_rows('hero') ): while( have_rows('hero') ): the_row();
if( have_rows('hero_-_background_options') ): while( have_rows('hero_-_background_options') ): the_row();
$image = get_sub_field('background_-_image');
if($image):
?>
.default-hero .col .hero-bkg {
    display:none;
}
<?php  endif; endwhile; endif; endwhile; else: ?>
.default-hero.relative.bg-grey-light {
  display: none;
}
<?php endif; endif; ?>

CodePudding user response:

Try this: else appeared to be at the wrong place.

<?php
if ( is_singular() || is_page() ):
if( have_rows('hero') ): while( have_rows('hero') ): the_row();
if( have_rows('hero_-_background_options') ): while( have_rows('hero_-_background_options') ): the_row();
$image = get_sub_field('background_-_image');
if($image):
?>
.default-hero .col .hero-bkg {
    display:none;
}
<?php else: ?>
.default-hero.relative.bg-grey-light {
  display: none;
}
<?php endif; endwhile; endif; endwhile; endif; endif; ?>
  • Related