gpt4 book ai didi

php - WP echo get_user_meta 单值打印 'array'

转载 作者:行者123 更新时间:2023-12-04 17:23:55 24 4
gpt4 key购买 nike

我正在尝试从我的 WP usermeta 表中显示给定 user_idline_managersnickname

表格如下所示:

user_id 2 具有 line_manager1

enter image description here

'user_id' 1 具有 nicknameMyNicknameString

enter image description here

期望的结果

User ID: 2
Line Manager ID: 1
Line Manager Nickname: MyNicknameString

PHP

User ID: 
<?php echo bp_displayed_user_id(); ?>
<br>

Line Manager ID:
<?php
$line_manager = get_user_meta( bp_displayed_user_id() , 'line_manager' , true );
echo $line_manager;
?>
<br>

Line Manager Nickname:
<?php
$line_manager = get_user_meta( bp_displayed_user_id() , 'line_manager' , true );
$line_manager_nickname = get_user_meta( $line_manager , 'nickname' , true );
echo $line_manager_nickname;
?>

当前结果

User ID: 2
Line Manager ID: Array
Line Manager Nickname:

如何正确引用这些键值?


编辑:var_dump($line_manager)users 表而不是预期的 usermeta 表返回数组数据。我原以为它会打印 usermeta


编辑 2:以下 SQL 的工作方式与我期望的 WP 函数相同。

global $wpdb,$table_prefix;
$displayed_user = bp_displayed_user_id();
$line_manager_ID = $wpdb->get_var("SELECT meta_value FROM ".$table_prefix."usermeta WHERE user_id = ".$displayed_user." AND meta_key = 'line_manager' ");
echo $wpdb->get_var("SELECT meta_value FROM ".$table_prefix."usermeta WHERE user_id = ".$line_manager_ID." AND meta_key = 'nickname' ");

最佳答案

只是一个想法,请参阅评论以更好地解释这里发生的事情......

@redditor在你的最后paste bin评论你得到 $line_manager_id 太早了,你需要确保 $user_meta['line_manager'][0] 在尝试获取这个 id 之前设置,否则你会收到错误或警告...请在下面查看我更新的答案并附上评论

<?php

// get current user id the wordpress way
// $user_id = get_current_user_id();

// get displayed user your way
$user_id = bp_displayed_user_id();

// get user meta data
$user_meta = get_user_meta($user_id);

// if we have user id
if($user_id) { ?>

User ID: <?=$user_id?>
<br/>

<?php // if line manager array key and sub key 0 is set ?>
<?php if(isset($user_meta['line_manager'][0])) { ?>

<?php // here it is safe to now get the line manager user meta ?>
<?php $lm_user_meta = get_user_meta($user_meta['line_manager'][0]); ?>

Line Manager ID: <?=$user_meta['line_manager'][0]?>
<br/>

<?php // check line manager user meta array nickname key and sub key 0 is set ?>
<?php if(isset($lm_user_meta['nickname'][0])) { ?>

Line Manager Nickname: <?=$lm_user_meta['nickname'][0]?>

<?php } ?>

<?php } ?>

<?php } ?>

关于php - WP echo get_user_meta 单值打印 'array',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64583254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com