
Hello, i was wondering if i could get some help with hiding this message when selected project is empty:
Warning
: Invalid argument supplied for foreach() in _projects.php
code:
<?php
$projects = get_field(‘projects’);
?>
<h2>Supported projects</h2>
<hr class=”bg__yellow”>
<div class=”posts”>
<?php foreach( $projects as $post) : setup_postdata($post); ?>
<?php if(get_field(‘landing_page’)) : ?>
“>
<?php else : ?>
<div class=”item”>
<?php endif; ?>
<div class=”inner”>
<?php if(get_field(‘landing_page’)) : ?>
<div class=”landing_page landing_page–project”>View Project</div>
<?php endif; ?>
<div class=”inner_image” data-aspect-ratio=”3:2″>
<div class=”image” style=”background-image:url(<?php echo get_field(‘main_image’)[‘sizes’][‘w600’]; ?>)”></div>
</div>
<div class=”inner_content”>
<h2>
<?php if(get_the_terms($post->ID, ‘pp_technology’)) : foreach(get_the_terms($post->ID, ‘pp_technology’) as $term) : ?>
<span class=”<?php echo ‘tech tech–‘ . $term->slug; ?>”></span>
<?php endforeach; endif; ?>
<?php the_title(); ?>
</h2>
<?php if(get_field(‘description’)) { the_field(‘description’); } ?>
</div>
</div>
<?php if(get_field(‘landing_page’)) : ?>
<?php else : ?>
</div>
<?php endif; ?>
<?php endforeach; wp_reset_postdata(); ?>
</div>
You could wrap it in an if
statement to check if $projects
is empty.
<?php
$projects = get_field('projects');
if ( $projects ) :
?>
<h2>Supported projects</h2>
<hr class=”bg__yellow”>
<div class="posts">
<?php foreach( $projects as $post) : setup_postdata($post); ?>
// Your code here...
<?php endforeach; wp_reset_postdata(); ?>
</div>
<?php endif; ?>