0

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' );
5
  • What happens if you do var_dump($full_tegnet)? And what does strlen($full_tegnet) return? Commented Apr 26, 2020 at 9:27
  • The simple equal sign is an assignment. This is only in exceptional cases in an IF and makes the code illegible.The double equal sign is the comparison operator and should match.As @MC Emperor said, what does var_dump print? Commented Apr 26, 2020 at 9:52
  • hmm.. For some reason, var_dump returns null. I have used get_field before with great success. Something like this: $tegnet = get_field("tegnede_andele_"); - $maxvalue = ($tegnet == 100) ? 0 : 10000; Commented Apr 26, 2020 at 9:55
  • ACF get_field Try to use get_field() with the post id and disable any formatting logic: <?php $full_tegnet = get_field('tegnede_andele_', get_the_ID(), false); If still null, then your field is really null! Commented Apr 26, 2020 at 10:26
  • it still returns null... However, I found out that the issue is that I am trying to get data outside the loop. If I echo something like this then it echos 100: global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'tegnede_andele_', true); wp_reset_query(); Commented Apr 26, 2020 at 11:00

2 Answers 2

1

assuming the $full_tegnet should be an integer you can use following

if(intval($full_tegnet) === 100){
     return '<h2>Something</h2>';
}
Sign up to request clarification or add additional context in comments.

Comments

0
  1. 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 }

  2. 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>'; }

  3. 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

  4. Use built in ACF shortcode to see what is being returned

    [acf field="{$tegnede_andele_}"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.