Support

Account

Home Forums General Issues Multiple if( in array… on one page not working.

Solving

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.

  • Could you specify the field type of “ingredients”?

  • 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 -->
  • This reply has been marked as private.
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Multiple if( in array… on one page not working.’ is closed to new replies.