Wordpress PHP command and ELSE IF problem

admin2025-06-06  6

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";
}

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";
}
Share Improve this question edited Nov 15, 2018 at 15:10 Johnny Dark asked Nov 15, 2018 at 14:26 Johnny DarkJohnny Dark 52 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/resources/the_field/ for more information.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749177651a317013.html

最新回复(0)