在我的网站上,当用户注册时,可以选择从其他选项中选择一个非营利性账户。一旦用户注册并查看该网站的特定页面,我希望使用获取其元信息的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 }?>
-----------------------新规----------------------
这使我可以看到联盟变量为用户提取的内容 结果如下:'用户隶属:汗学院'
<?php global $current_user;
get_currentuserinfo();
echo 'User Affiliation: ' . $current_user->affiliation . "\n";
?>
答案 0 :(得分:0)
你可以将meta传递给会话var,即$ _SESSION [&#39; havemeta5&#39;];
答案 1 :(得分:0)
get_user_meta
设置错误。试试这个:
$havemeta1 = get_user_meta( $userID, 'nch', true );
......等等。您需要将$userID
作为get_user_meta()
的第一个参数传递 - 而不是$affiliation
。
<强>更新强>
如果您的用户元是'affiliation'(允许您拨打$ current_user-&gt; 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>';
}