questions

ACF – display div based on select value

Rate this post

I’m trying to trigger variable content based on the value set on a select custom field on a page. If the option selected is “Header standard” then display a div.

The code I’m using is below. Any help would be appreciated.

<?php $featured_post = get_field('test_header_field'); ?>

<?php if(the_field('test_header_field', $featured_post) == "Header Standard"): ?>
    <div>The Content if test_header_field is equal to Header Standard.</div>
<?php endif; ?>

 

✔️Solution:

You can refactor your code to something like below:

<?php
// grab acf field
$featured_post = get_field('test_header_field');

// set bool based on featured_post text
$is_header_standard = $featured_post === "Header Standard";
?>

<?php 
// conditionally display based on previously set bool
if ($is_header_standard) : ?>
    <div>The Content if test_header_field is equal to Header Standard.</div>
<?php endif; ?>

Leave a Reply

Your email address will not be published.

Back to top button