在我的网站上,当用户注册时,有一个选项可以从一个额外的选项中选择,该选项会将他们的帐户与一个非营利性组织联系起来。一旦用户注册并查看网站的特定页面,我希望该网站是为他们量身定做使用php,抓住他们的元信息。为此,我将回显一个按钮,该按钮根据注册时选择的元值来定制前端。
如果它们没有元键,则不会显示任何内容。
这是我的代码尝试,但不起作用!
<?php global $current_user;
get_currentuserinfo(); //wordpress global variable to fetch logged in user info
$userID = $current_user->ID; //logged in user's ID
$havemeta1 = get_user_meta($userID,'nch',true); //stores the value of logged in user's meta data for 'National Coalition for the homeless'
$havemeta2 = get_user_meta($userID,'rotary-international',true); //stores the value of logged in user's meta data for 'Rotary International'
$havemeta3 = get_user_meta($userID,'khan-academy',true); //stores the value of logged in user's meta data for 'Khan Academy'
$havemeta4 = get_user_meta($userID,'wwf',true); //stores the value of logged in user's meta data for 'World Wildlife Fund (WWF)'
$havemeta5 = get_user_meta($userID,'bcrf',true); //stores the value of logged in user's meta data for 'The Breast Cancer Research Foundation'
?>
<!--add if statement to figure out what button to show to logged in user-->
<?php if ($havemeta1) { ?>
<div <p>nch</p> class="Button1"></div>
<?php } elseif ($havemeta2) { ?>
<div <p>rotary-international</p>class="Button2"></div>
<?php } elseif ($havemeta3) { ?>
<div <p>khan-academy</p>class="Button3"></div>
<?php } elseif ($havemeta4) { ?>
<div <p>wwf</p>class="Button4"></div>
<?php } elseif ($havemeta5) { ?>
<div <p>bcrf</p>class="Button5"></div>
<?php } else { ?>
<div><p>None - No Matching Affiliation</p></div>
<?php }?>这让我可以看到从属变量为用户拉出了什么,结果是:'User Affiliation: khan-academy‘。
<?php global $current_user;
get_currentuserinfo();
echo 'User Affiliation: ' . $current_user->affiliation . "\n";
?>发布于 2014-10-20 05:49:26
你能传递一个会话变量,即$_ session 'havemeta5';
发布于 2014-10-20 07:58:15
get_user_meta设置错误。试试这个:
$havemeta1 = get_user_meta( $userID, 'nch', true );...and等等。您需要将$userID作为get_user_meta()的第一个参数传递,而不是$affiliation。
更新
如果您的用户meta是'affiliation‘(允许您调用$current_ user ->affiliation),您可以这样做:
<?php
global $current_user;
get_currentuserinfo();
$userID = $current_user->ID;
$affiliation = get_user_meta( $userID, 'affiliation', false );
$affiliation = $affiliation[0];
if( 'nch' == $affiliation ){
print '<div class="Button1"><p>nch</p></div>';
} elseif( 'rotary-international' == $affiliation ){
print '<div class="Button2"><p>Rotary International</p></div>';
} elseif( 'khan-academy' == $affiliation ){
print '<div class="Button3"><p>Khan Academy</p></div>';
} elseif( 'wwf' == $affiliation ){
print '<div class="Button4"><p>wwf</p></div>';
} elseif( 'bcrf' == $affiliation ){
print '<div class="Button5"><p>bcrf</p></div>';
} else {
print '<div><p>None - no matching affiliation</p></div>';
print '<div>User ID: '.$userID.', Affiliation: '.$affiliation.'</div>';
}https://stackoverflow.com/questions/26455684
复制相似问题