[英]Displaying user meta data (array) only displaying first value
我通過 webhooks(使用 wp-webhook 插件)將電話詳細信息添加到相應用戶的元數據中。 輸出是一個數組,因為數據總是被添加。 由於某種原因,只有第一個值顯示在前端。 下面是我嘗試過的兩種不同的方法,但都沒有顯示所有數據。 我也在使用 PHP Code Snippets 插件(不確定這是否與問題有關)。 用戶配置文件中顯示的 webhook 的輸出元數據如下所示:
//output data
array (
0 => 'phone_data 1',
1 => 'phone data 2',
2 => 'phone data 3',
3 => 'phone data 4',
4 => 'phone data 5',
5 => 'phone data 6',
)
//get and display user meta
$current_user = wp_get_current_user();
if ( $current_user ) {
$meta = get_user_meta( $current_user->ID, 'phone_calls' , true );
if ( ! is_array( $meta ) ) {
$meta = array();
}
echo 'User phone calls: ' . $current_user->phone_calls . ;
}
// Also Tried this:
$current_user = wp_get_current_user();
echo 'User phone calls: ' . $current_user->phone_calls . ;
你的代碼壞了。 這是它的一個有效版本,即使您可以使用 WordPress 內部函數get_current_user_id()來簡化它。
//get and display user meta
$current_user = wp_get_current_user();
if ( $current_user ) {
$meta = get_user_meta( $current_user->ID, 'phone_calls' , true );
if ( ! is_array( $meta ) ) {
$meta = array();
}
echo 'User phone calls: ' . json_encode( $meta );
}
您還可以使用以下功能(直接在 WP Webhooks 中)獲取用戶元數據: https ://wp-webhooks.com/integrations/wordpress/actions/get_user/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.