$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Wordpress PHP command and ELSE IF problem|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Wordpress PHP command and ELSE IF problem

matteradmin7PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far