PHP if statement: Zero vs 'empty' - php

I'm creating a WordPress website using the Advanced Custom Fields plugin. I have a set of fields for tennis score results. In the template, I'm showing these fields like this:
<?php if(get_field('my_field')) : ?>
<?php echo get_field('my_field'); ?>
<?php endif; ?>
The problem is that some scores are zero, so they're not showing up. I understand that this is because 0 basically equals null, so the statement is false.
One solution I found and tried was this:
<?php if(get_field('my_field') !== false) : ?>
<?php echo get_field('my_field'); ?>
<?php endif; ?>
However, this means that empty fields now show up too, which is not desirable since there are a lot of fields that are intended to be hidden if empty.
So, my question is, is there a way to phrase an if statement that allows for zeros, while still returning false if the field is empty? Please note that some scores aren't purely numeric, with values like '6(1)'.

In order to check for empty strings you have to explicitly check them in your if condition.
<?php if(get_field('my_field') !== '') : ?>
<?php echo get_field('my_field'); ?>
<?php endif; ?>
The reason is 0, null, empty string, empty array all evaluate to (but are not exactly) false, in case of a boolean check.

Related

Using get_field with specific postID returns empty

I'm trying to use get_field in a loop to retrieve some custom field values but
when using get_field('container', post_id) the value is always empty.
I tried to even use it inside the block file and the same happens.
$container = get_field('container'); // this works
$test_container = get_field('container', 144); //this returns empty, post id === 144
Did you try the_field() function.
In ACF the_field() function is used to display the value of a specific field. it is same as get_field().
https://www.advancedcustomfields.com/resources/the_field/
<?php if( get_field('container') ): ?>
<p><?php the_field('container', 144); ?> </p>
<?php endif; ?>
The code you using should work but if it's not working for any reason. You can also use
get_post_meta(); //to retrieve the value.

php while statement, except on post id

i got the following php. and what i want is this loop to exclude specific post ids
The following field will generate the id i wish to exclude
<?php the_field('excludeid'); ?>
(this field basicly checks if a checkbox is marked on the page, and if it is, it will output the page id)
So if it checks page id 701, checkbox is marked, it will output 701 (and otherwise it outputs nothing)
so in the loop i want to check if the page it loops for, matches with that output, if it does, it needs to skip it
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
Do something, except when page id is equal to <?php the_field('excludeid'); ?>
<?php endwhile; ?>
Edit, this is what i have now:
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<?php if( $post->ID == 686) continue; ?>
<?php endwhile; ?>
That works, but when i change the 686 to "the_field('excludeid')" (which outputs 686) it doesnt work.
I am a total newb in php, so manybe i am doing something dumb here XD
while cycle is a type of conditional, but in loop, you can set any type of logic inside its arguments. In this case you iterate about $parent->have_post()
while($parent->has_post() && (!in_array($parent->the_post()->id, the_field('excludeid')) {
// your logic
}
I don't know how is your business code, but is that, you must know that while is that a conditional, but it loop

PHP/Wordpress: IF/ELSE executing both conditions

Have been reading through multiple similar questions and went over my syntax many times, but I can't figure out why my PHP code is executing both conditions.
I'm trying to replace the url of an element with the string from a custom field if the field is not empty. If the field is empty, I want to output the permalink normally. What happens is that the code concatenates both the string from the custom field and the permalink when the field is not empty. If i remove the string in the field, it works fine.
<div class="profile-content">
<a href="
<?php
if ( the_field('direct_resource_link') != '') {
the_field('direct_resource_link');
} else {
the_permalink($id);
} ?>
"><h5><?php the_title(); ?></h5></a>
<div class="profile-footer">
Thanks!
Dan.
EDIT after comment from original poster
My initial assessment (left below for reference) was correct. You are using function that will print/echo content instead of returning it. Your if will always evaluate to false, because you are calling function that returns nothing; and PHP thinks that nothing and empty string are the same thing.
You didn't see that when field was empty, because the_field() for empty field printed empty string (or nothing at all), i.e. it didn't modify value printed by the_permalink() in any way/
According to ACF documentation, the_field() is accompanied by get_field() which returns value instead of printing it.
Your code should look like that:
<div class="profile-content">
<a href="
<?php
if ( get_field('direct_resource_link') ) {
the_field('direct_resource_link');
} else {
the_permalink($id);
} ?>
"><h5><?php the_title(); ?></h5></a>
<div class="profile-footer">
My initial post
What happens is that you run function the_field('direct_resource_link') and compare it's return value to ''; if that value is empty, you run the_permalink($id);.
It's hard to tell what the_field() is and what it is supposed to do, but I guess that it prints value instead of returning it. So if field is empty, it prints nothing, resulting in pure run of the_permalink(). If field is not empty, it prints it content and returns nothing. Since nothing is equal to empty string, PHP proceeds with else branch and invokes the_permalink() that prints additional info.
Solution: modify the_field() to return value instead of printing it, or make additional function that will query for value and return it, and use that function in if statement.
Miroslaw Zalewski already answered your question here, so this is simply to show you the kind of code needed to fix your issue:
function get_the_field($field) {
ob_start();
the_field($field);
return ob_get_clean();
}
This code will start an output buffer (which will capture all echo'd data), run the_field and return (and delete) the output buffer (the echo'd data from the_field). This means you can simply do the following:
...
<?php
$theField = get_the_field('direct_resource_link');
if ( $theField != '') {
echo $theField;
} else {
the_permalink($id);
}
?>
...
This can all be simplified. the_field() echoes a meta value. You don't want that...Instead, you want to return the value to check it, before conditionally echoing it. You can do this using get_field().
In the simplest form, your final code would look like the following:
<a href="<?php get_field('direct_resource_link') ? the_field('direct_resource_link') : the_permalink($id); ?>">
<h5><?php the_title(); ?></h5>
</a>

PHP Show menus/submenus according to the value of a variable

I have a navigation bar in which I am trying to show menus/buttons, according to the type of user. I get the type of user via a variable called $isManager.
The good news is that it works on every browser, except firefox.
Code looks like this:
<?php
if ($isManager === '2'){
?>
<li>View</li>
<?php
}
?>
Can you suggest an alternative to this, or is Firefox somehow ignoring or not accepting the true condition here ?
When you use ===, it is for strict checking. So make sure that your$isManager is string type. If it is integer then try
<?php
if ($isManager === 2){
?>
<li>View</li>
<?php
}
?>
You are Using === it means you want to check by its typeof too.
and after that you wrote '2', so it will missmatch the results and not going to the condition, instead try the following.
<?php
if ($isManager === 2){
?>
<li>View</li>
<?php
}
?>

Hiding a custom field if no value is submitted

How can I hide a specific field on a page displaying data from a form if no value is submitted? An example is below:
<?php if($price): ?>
<li><?php echo $price;?>: <?php echo get_property_price($post->ID);?> <?php echo get_post_meta($post->ID,'rentperiod',true);?></li>
<?php endif; ?>
I don't want the $price or rentperiod values to display as a list item if they are empty. What's the best way to accomplish this?
Get the data first, then display it conditionally. Also avoid changing too often between php and html to get more readable code.
Use the variable expansion in double quotes strings.
$property_price = get_property_price($post->ID);
$rentperiod = get_post_meta($post->ID,'rentperiod',true);
if($price && ($property_price || $rentperiod))
echo "<li>$price: $property_price $rentperiod</li>";

Categories