Home › Forums › General Issues › Multiple if( in array… on one page not working.
Trying to get multiple if( in array for a single field. So far it only shows the first one.
<?php if( in_array( 'Barley', get_field('ingredients') ) ) {
// WP Loop
$rand_posts = get_posts('cat=2&numberposts=1&orderby=rand');
foreach( $rand_posts as $post ) : ?>
<?php the_title( '<h1 class="entry-title">', '</h1>' );?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'full' ); } ?>
<?php endforeach;
// End Loop
} ?>
<?php if( in_array( 'Water', get_field('ingredients') ) ) {
// WP Loop
$rand_posts = get_posts('cat=3&numberposts=1&orderby=rand');
foreach( $rand_posts as $post ) : ?>
<?php the_title( '<h1 class="entry-title">', '</h1>' );?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'full' ); } ?>
<?php endforeach;
// End Loop
} ?>
Any help would be greatly appreciated.
It’s a checkbox field. But now that I look at this I think it might be because I didn’t start a new loop rather than it being an issue with ACF.
Nope, that wasn’t it. If I take away the ACF the two WP loops work perfectly. But when I put them back only the first loop works.
New Code to be safe separating the loops
<!-- ACF Start -->
<?php if( in_array( 'Barley', get_field('ingredients') ) ) { ?>
<!-- WP Loop Start -->
<?php $BarleyQuery = new WP_Query( 'category_name=Barley&posts_per_page=1&orderby=rand' ); ?>
<?php while ( $BarleyQuery->have_posts() ) : $BarleyQuery->the_post(); ?>
<!-- Post Contents -->
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'full' ); } ?>
<!-- End Post Contents -->
<?php endwhile; ?>
<!-- WP Loop End -->
<?php }; ?>
<!-- ACF End -->
<!-- ACF Start -->
<?php if( in_array( 'Water', get_field('ingredients') ) ) { ?>
<!-- WP Loop Start -->
<?php $WaterQuery = new WP_Query( 'category_name=Water&posts_per_page=1&orderby=rand' ); ?>
<?php while ( $WaterQuery->have_posts() ) : $WaterQuery->the_post(); ?>
<!-- Post Contents -->
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'full' ); } ?>
<!-- End Post Contents -->
<?php endwhile; ?>
<!-- WP Loop End -->
<?php }; ?>
<!-- ACF End -->
The topic ‘Multiple if( in array… on one page not working.’ is closed to new replies.
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.