Support

Account

Home Forums ACF PRO If relationship has only 1 post do X, if not Y.

Solved

If relationship has only 1 post do X, if not Y.

  • I’m looking for a bit of help figuring out how to query the relationship field to first check if it contains more than one value, if so – i want to do one thing, if there is only 1 value, i’d like it to do another. code attached below:

    <!-- COURSE FACULTY -->
            <?php
            global $post;
            $faculty = get_field('alt_faculty');
            if( $faculty ): ?>
            <h3>Course Faculty</h3>
            <?php foreach( $faculty as $post): ?>
             <?php setup_postdata($post); ?>
             <div class="post-author-wrap m-1of2 t-1of3 d-1of3 cf">
               <div class="post-author cf">
                 <div class="thumb">
                   <?php if ( has_post_thumbnail() ) { ?>
                   <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                   <?php } else { ?>
                   <a href="<?php the_permalink(); ?>"><img src="<?php echo catch_that_image(); ?>" /></a><?php }?>
                 </div>
                 <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                 <div class="thecontent">
                 <?php the_excerpt(); ?>
               </div>
               </div>
             </div>
           <?php endforeach; ?>
           <?php wp_reset_postdata(); ?>
          <?php endif; ?>
    
    <!-- END COURSE FACULTY -->
  • you just need an else

    
    $faculty = get_field('alt_faculty');
    if ($faculty && count($faulty) == 1):
      // only 1
    elesif ($faculty):
      // more than 1
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘If relationship has only 1 post do X, if not Y.’ is closed to new replies.