如果是開發一個多人的wordpress網站的話,就需要在適當的位置展示不同用戶的頭像和昵稱等用戶信息了,那么我們在開發wordpress主題時該如何獲取這些信息呢?這一次的wordpress開發教程就和大家分享一下。

獲取作者頭像需要用到的兩個函數
獲取用戶頭像的函數是get_avatar
,該函數以用戶 ID 或 Email 信息為依據,獲取用戶在 Gravatar 網站上的通用頭像。
而用戶 ID 或 Email 信息,我們可以通過get_the_author_meta
函數獲取。該函數需要兩個參數,一個是我們需要獲取的用戶字段,一個是用戶 ID。如果是在文章循環中,第二個參數是不需要的,默認就是該文章作者的用戶 ID。
獲取作者用戶頭像的代碼示例
結合上面的兩個函數,我們就可以非常輕松的獲取用戶的頭像了。
<?php echo get_avatar( get_the_author_meta( 'user_email' ) ); ?>
或者通過用戶ID獲取也行,兩個函數是等效的。
<?php echo get_avatar( get_the_author_meta( 'ID' ) ); ?>
獲取文章的作者名稱和鏈接
<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php the_author(); ?></a>
獲取文章作者的其他信息
除了用戶頭像之外,我們可能還會需要文章作者的其他信息,獲取這些信息非常簡單,只需要給get_the_author_meta
函數傳入對應的參照即可,可用的參數有:
- user_login
- user_pass
- user_nicename
- user_email
- user_url
- user_registered
- user_activation_key
- user_status
- roles
- display_name
- nickname
- first_name
- last_name
- description?(作者簡介)
- jabber
- aim
- yim
- googleplus
- user_level
- user_firstname
- user_lastname
- rich_editing
- comment_shortcuts
- admin_color
- plugins_per_page
- plugins_last_view
- ID
除了這些基本的用戶信息,插件和主題可能會添加附件的用戶信息為用戶自定義字段,對于主題或插件添加的用戶信息,我們可以使用 get_user_meta
函數獲取。
參考來源:https://www.wpzhiku.com/show-user-avatar-nickname-and-other-profile-in-post/
如果是需要在前臺更改頭像的話,可以看看這篇wordpress開發教程: