Home › Forums › General Issues › String Comparison if statement, boolean comparison don't work on if statement
I feel kind of dumb for asking this pretty basic question but I’ve tried several things, read a around a bunch and can’t seem to figure out what I’m doing wrong.
$loop = new WP_Query( array( 'post_type' => 'side_project', 'orderby' => 'post_id', 'order' => 'ASC' ) ); ?>
<?php
//While loop cycling through sideproject posts.
while( $loop->have_posts() ) : $loop->the_post();
//VARIABLES
$centerVertical = get_field('center_vertical'); ?>
<figure class="side-project-box lazy" style="<?php $field = 'overlay'; echo tag_stripped_field($field); ?>">
<div class="img-container">
<?php
if ( $centerVertical === "vertical") {?>
<img class="side-project-img lazy" style="top:-300px;, right:100px;" src="<?php echo get_the_post_thumbnail_url() ?>" />
<?php} else {?>
<img class="side-project-img lazy" src="<?php echo get_the_post_thumbnail_url() ?>" />
</figure>
<?php}?>
It’s very simple. I have some images that serve as backgrounds for some blog preview links. Everything has been working fine, but I decided I wanted to add an option to tamp the center down a bit if the image was extremely vertical in it’s aspect ratio.
At first I tried a true/false output. I would echo the $centerVertical into a <p> so I could see what it was outputting. For true, it was outputting 1, for false, nothing. I assume null but having it check for either 1 or null or 0 still lead to the same empty screen that wouldn’t load anything else on the page.
Second I tried radio buttons as you can see here. I get an explicit output of “vertical” or “none” and yet when I check for vertical I still get a broken webpage where nothing loads. I know it’s this particular if statement because I can put it in and take it out and it breaks it each time. I know the field is properly outputting because I have echoed it into visible HTML to check. But I still can’t find it. It’s just acting wonky.
Not sure if these will be a complete solution, but here are a few things I noticed:
1. Try adding white space after your opening php tags:
<?php} else {?> // Incorrect
<?php } else { ?> // Correct
<?php}?> // Incorrect
<?php } ?> // Correct
2. Looks like you are missing a closing div for .img-container.
3. Does your code include an endwhile?
Yeah it has an endwhile. And there’s a closing div for img container I just accidently didn’t copy it over. I ended up getting it to work but by doing this:
if ( 'vertical' == get_field('center_vertical') ): ?>
<img class="side-project-img lazy" style="top:-300px; right:100px;" src="<?php echo get_the_post_thumbnail_url() ?>" />
<?php else: ?>
<img class="side-project-img lazy" src="<?php echo get_the_post_thumbnail_url() ?>" />
<?php endif;?>
Which is just a piece of code I copied from elsewhere on the forum so I think it was just some syntax issue I was overlooking, probably the spaces as you said.
You must be logged in to reply to this topic.
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!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.