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


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 -->
            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 }?>
                 <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                 <div class="thecontent">
                 <?php the_excerpt(); ?>
           <?php endforeach; ?>
           <?php wp_reset_postdata(); ?>
          <?php endif; ?>
  • 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)

You must be logged in to reply to this topic.