Support

Account

Home Forums Front-end Issues Conditional statement for relationship field? Reply To: Conditional statement for relationship field?

  • Ah yes that worked! I was just playing around with it and it seems like the following works too. Are they both valid ways of writing the code, or is one better syntax than the other?

    
    <?php 		
    	// get only first 1 results
    	$ids = get_field('testimonials_relationship', false, false);			
    	
    	$the_query = new WP_Query(array(
    		'post_type'      	=> 'testimonials',
    		'posts_per_page'	=> 1,
    		'post__in'			=> $ids,
    		'post_status'		=> 'any',
    		'orderby'        	=> 'rand',
    	));	
    ?>
    	
    	<?php if ( $the_query->have_posts() ) : ?>				
    	
    		<!-- the loop -->
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		
    			<?php
    				// Must be inside a loop.							 
    				// If there are testimonials chosen display:
    				if ($ids) { ?>
    					
    					<h2><?php the_title(); ?></h2>
    					
    				<?php } else  { ?>							    
    					<!-- No testimonials for this page -->
    				<?php } ?>
    			
    		<?php endwhile; ?>
    		<!-- end of the loop -->
    	    
    		<?php wp_reset_postdata(); ?>
    	
    	<?php else : ?>
    		<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
    	<?php endif; ?>