将额外字段添加到WordPress用户配置文件

问题描述:

我有一个奇怪的问题。我已将自定义字段添加到用户配置文件,并使用the_author_meta()函数显示字段内容。 Yeasterday一切都很棒。但今天它不起作用。将额外字段添加到WordPress用户配置文件

自定义字段在用户配置文件中。当我点击更新配置文件时 - 字段的内容仍然存在(因此应该保存信息)。但在作者页上,我有<?php the_author_meta('skills'); ?>什么也没有发生。

我跟着这个教程中,当我创建自己的自定义字段: http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields

而这一次,当我试图找到我的代码中的错误: http://bavotasan.com/2009/adding-extra-fields-to-the-wordpress-user-profile/

他们都是相似的。我从昨天开始一直使用页面模板,所以可能会导致问题。所以我删除了自昨天以来我添加的所有内容。但没有任何改变。

这里是我的代码:

// CUSTOM FIELD IN USER ADMIN - SAVE 
add_action('personal_options_update', 'my_save_extra_profile_fields'); 
add_action('edit_user_profile_update', 'my_save_extra_profile_fields'); 


function my_show_extra_profile_fields($user) { ?> 

    <h3>Skills - Färdigheter</h3> 

    <table class="form-table"> 

     <tr> 
      <th><label for="skills">Skills</label></th> 

      <td> 
     <textarea type="text" name="skills" rows="4" cols="50" id="skills" class="regular-text"><?php echo esc_attr(get_the_author_meta('skills', $user->ID)); ?></textarea><br>  
       <span class="description">Please enter your skills.</span> 
      </td> 
     </tr> 

    </table> 
<?php } 

// CUSTOM FIELD IN USER ADMIN 
add_action('show_user_profile', 'my_show_extra_profile_fields'); 
add_action('edit_user_profile', 'my_show_extra_profile_fields'); 

function my_save_extra_profile_fields($user_id) { 

    if (!current_user_can('edit_user', $user_id)) 
     {return false;} 

    /* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */ 
    update_user_meta($user_id, 'skills', $_POST['skills']); 
} 
+0

我试过你的代码,它工作正常。你确定你在调用' [内循环](https://codex.wordpress.org/The_Loop)? – d79 2015-04-01 02:10:36

现在,我认为你会成功保存用户的元数据。只有你需要显示他们。

现在您可以使用函数“get_user_meta($ user_id,$ key,$ single);”。 请访问它:https://codex.wordpress.org/Function_Reference/get_user_meta 谢谢

+0

感谢您的建议,但它不起作用(也许因为我没有循环..我不知道)。我使用'echo $ curauth-> skills',它可以工作。 – Bogdan 2015-04-01 10:11:29

问题解决了!该解决方案是使用<?php echo $curauth->skills; ?>,而不是<?php the_author_meta('skills'); ?><?php get_user_meta($user_id, $key, $single); ?>

我不知道为什么,但它并不重要:d