2015-03-31 115 views
1

我有一個奇怪的問題。我已將自定義字段添加到用戶配置文件,並使用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

我試過你的代碼,它工作正常。你確定你在調用'<?php the_author_meta('skills'); ?> [內循環](https://codex.wordpress.org/The_Loop)? – d79 2015-04-01 02:10:36

回答

0

現在,我認爲你會成功保存用戶的元數據。只有你需要顯示他們。

現在您可以使用函數「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

0

問題解決了!該解決方案是使用<?php echo $curauth->skills; ?>,而不是<?php the_author_meta('skills'); ?><?php get_user_meta($user_id, $key, $single); ?>

我不知道爲什麼,但它並不重要:d

相關問題