Reputation: 625
I am using a PHP function with a simple if else statement. If variable = 100 do something, else do something else.
The data is coming from an ACF Range Field. For some reason, the function always returns the else-statement even though the ACF field is = 100. I figured the problem is the if-statement where I've tried to use: =, ==, !==, === or >=. If I change it to single = then it always returns h2 Something but all the rest returns h2 something else no matter what value I put in the ACF Range field.
function hovsa_shortcode() {
$full_tegnet = get_field("tegnede_andele_");
if ( $full_tegnet == '100' ) {
return '<h2>Something</h2>';
} else {
return '<h2>Something else</h2>';
}
}
add_shortcode( 'hovsa', 'hovsa_shortcode' );
Upvotes: 0
Views: 457
Reputation: 156
According to documentation ACF Range field is a numeric value. As stated by @Daisen Sekai, you can cast $full_tegnet
by using intval()
in your condition and use strict equality for comparison.
<?php
$full_tegnet = intval($full_tegnet);
if ($full_tegnet === 100) { // your logic }
But PHP does type juggling and if $full_tegnet = 100
, your statement if ( $full_tegnet == '100' )
should return true. You can test this piece of code in a script and see the result:
<?php
$full_tegnet = 100;
if ( $full_tegnet == '100' ) {
echo '<h2>Something</h2>';
} else {
echo '<h2>Something else</h2>';
}
There may be other issues in your code, more possibly value of $full_tegnet
that is causing this. As commented by @MC Emperor, do a var_dump($full_tegnet)
to get value/type of $full_tegnet
Use built in ACF shortcode to see what is being returned
[acf field="{$tegnede_andele_}"]
Upvotes: 0
Reputation: 163
assuming the $full_tegnet
should be an integer you can use following
if(intval($full_tegnet) === 100){
return '<h2>Something</h2>';
}
Upvotes: 1