繁体   English   中英

PHP的回声WordPress的用户元数据

[英]php echo wordpress user meta data

在我的网站上,当用户注册时,有一个选项可以从其他选项中进行选择,该选项可以使他们的帐户与非营利组织建立关系。 用户注册并查看网站的特定页面后,我希望使用获取其元信息的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";
?>

您可以将元数据传递到会话变量中吗,即$ _SESSION ['havemeta5'];

get_user_meta设置错误。 尝试这个:

$havemeta1 = get_user_meta( $userID, 'nch', true );

...等等。 您需要传递$userID作为get_user_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>';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM