0

I am trying to show different messages based on user's custom field data. 'Site-access' meta field can be 0 or 1 and I was able to retrieve it for logged in user using

 the_field('site-access', wp_get_current_user()); 

Now I have to apply a comparison statement to this command based on retrieved data. Unfortunately it doesn't work within ELSE IF tags and I can't assign it to variable.

$access = the_field('site-access', wp_get_current_user()); 
if($access == "1"){ 
echo 'welcome'; 
} 
else { 
echo 'access denied'; 
}

Working code

$access = get_field('site-access',  'user_' . get_current_user_id());
$int = (int)$access;
echo $int;
if ($int == "1") {
echo "it's 1";
} else {
echo "it's not 1";
}

1 Answer 1

1

It could work like this; but you need to use get_field() instead of the_field(), since the latter displays the field instead of returning a value. See https://www.advancedcustomfields.com/resources/the_field/ for more information.

4
  • Thanks Chris! I was able to get the data using get_field and assign it to a variable. But still can't put it to else if. $access = get_field('gym-access', 'user_' . get_current_user_id()); echo $access; Nov 15, 2018 at 15:02
  • if ($access == "2") { echo "it's 1"; } else { echo "it's not 1"; } Returns = It's not 1 Nov 15, 2018 at 15:04
  • I've converted data to integrer and now it seems to work! Thanks again, Chris! Nov 15, 2018 at 15:09
  • Welcome, @JohnnyDark - this should be interesting for you: php.net/manual/en/types.comparisons.php Nov 16, 2018 at 13:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.