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; ?>
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.