Home › Forums › Add-ons › Flexible Content Field › Echo nothing if value is X
Hi,
I currently have a Field Group comprising one Flexible Content unit, within which is a single Layout comprising multiple field types.
Specifically, one of those field types, Horizontal Alignment (jumbotron_horizontal_alignment), is a Radio Button. It should have two values for output…
In other words, Left is the default alignment option and should write out no value on the site. I want the function (the_sub_field?) to write out, in to my page.php, like this…
How do I account for the conditionality in this? Is that just straight PHP or what?
Pretty much yes, although I’d set the values of these fields to the actual class names if that I want to output so that I can simplify the code, you have not already
if (get_field('jumbotron_horizontal_alignment') != 'left') {
the_field('jumbotron_horizontal_alignment');
}
I’m afraid I don’t quite understand the solution.
Use case (refined) is:
jumbotron_horizontal_alignment could be either “Left” or “Centre”.
I modified like this…
if (get_field('jumbotron_horizontal_alignment') = 'Centre') {
echo 'd-flex text-center justify-content-center';
}
… But WordPress threw this…
Fatal error: Can't use function return value in write context in /home/mysite/public_html/wp-content/themes/mytheme/page.php on line 32
For the record, I am having a little success with the following…
<div class="col-md-12 cxt-title <?php
// if "centre" radio selected, use relevant bootstrap 4 classes
if( get_sub_field('jumbotron_horizontal_alignment') == 'Centre' ):
echo 'd-flex text-center justify-content-center';
elseif( get_sub_field('jumbotron_horizontal_alignment') == 'Left' ):
echo '';
endif;
?>"><!-- for left align, remove d-flex text-center justify-content-center -->
That is – jumbotron_horizontal_alignment is a radio button with possible values “Centre” or “Left”. If “Centre”, echo out the three Bootstrap 4 classes that centre-align the text. If “Left”, echo nothing.
I am learning a bit more ACF today, winging it a bit.
(Meanwhile, also learning Bootstrap 4! I’m not certain that left alignment shouldn’t also be controlled using text-left and justify-content-start, but left seems to be the default).
The above portion forms part of my overall Jumbotron layout…
<?php
// check if the flexible content field has rows of data
if( have_rows('page_components') ):
// loop through the rows of data
while ( have_rows('page_components') ) : the_row();
if( get_row_layout() == 'jumbotron' ): ?>
<!-- jumbotron -->
<div class="jumbotron jumbotron-fluid mb-0 cxt-bgimg <?php the_sub_field('jumbotron_height');?> cxt-bgimg-tint-default <?php the_sub_field('background_image');?> text-white d-flex justify-content-center">
<div class="container align-self-center">
<div class="row">
<div class="col-md-12 cxt-title <?php
// if "centre" radio selected, use relevant bootstrap 4 classes
if( get_sub_field('jumbotron_horizontal_alignment') == 'Centre' ):
echo 'd-flex text-center justify-content-center';
elseif( get_sub_field('jumbotron_horizontal_alignment') == 'Left' ):
echo '';
endif;
?>"><!-- for left align, remove d-flex text-center justify-content-center -->
<div class="align-self-center p-3">
<h1 class="display-4 pb-2"><?php the_sub_field('jumbotron_title');?></h1>
<p class="lead pb-4"><?php the_sub_field('jumbotron_subtitle');?></p>
<?php
// check if the flexible content field has rows of data
if( have_rows('button') ):
// loop through the rows of data
while ( have_rows('button') ) : the_row();
if( get_row_layout() == 'button' ):
echo '<a class="btn btn-primary pmd-ripple-effect btn-lg" href="' . get_sub_field('button_url') .'" role="button">'. get_sub_field('button_title') .'</a>';
endif;
endwhile;
endif;
?>
</div>
</div>
</div>
</div>
</div>
<?php endif; ?>
<!-- Other layouts snipped from here -->
<?php endwhile;
else :
// no layouts found
echo 'No ACF layouts';
endif;
?>
You modification is causing the error, it should be (note the double ==)
if (get_field('jumbotron_horizontal_alignment') == 'Centre') {
echo 'd-flex text-center justify-content-center';
}
I’m not sure, did you get it working. In my original reply I was just saying that you need to use get_field()
instead of the_field()
and add conditional blocks of code. The exact way these are added will depend on what you want to achieve, but given what you supplied in the op the best I could do was a simple example.
Looking at this
<div class="col-md-12 cxt-title <?php
// if "centre" radio selected, use relevant bootstrap 4 classes
if( get_sub_field('jumbotron_horizontal_alignment') == 'Centre' ):
echo 'd-flex text-center justify-content-center';
elseif( get_sub_field('jumbotron_horizontal_alignment') == 'Left' ):
echo '';
endif;
?>">
it does appear the you have the basic idea. You can probably clean it up a bit and make it easier to read by getting the sub field just once and then using that value in your if statements
$halign = get_sub_field(jumbotron_horizontal_alignment);
if ($halign == 'left') { .....
The topic ‘Echo nothing if value is X’ is closed to new replies.
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.