比@Brady向您展示的更简单的方法是使用current_user_can:
if ( current_user_can( \'subscriber\' ) )
echo "Hi, dear subscriber! Glad seeing you again!";
MU也有一个等效的MU安装,名为
current_user_can_for_blog:
global $blog_id;
if ( current_user_can_for_blog( $blog_id, \'subscriber\' ) )
echo "Hi, dear subscriber! Glad seeing you again on this blog!";
查看功能来源时,请在幕布后面
for single 或
MU installations, 然后你会看到,这两者基本上都依赖于
wp_get_current_user() 然后检查
has_cap.现在如果你想看看帽子是从哪里来的,那么
WP_User 类/对象进入游戏。
这一组的其他成员author_can( $GLOBALS[\'post\'], \'capability\' );. 所有这些功能都在里面~/wp-includes/capabilities 就在彼此的正下方。
什么时候使用什么
现在,两者的区别在哪里
current_user_can(_FOR_BLOG) 和
user_can?
user_can() 是较新的一个(自3.1以来),但需要用户作为对象。因此,您可以在不希望以当前用户为目标,但希望以某些用户为目标的情况下使用它current_user_can_*() 这是显而易见的author_can() 允许您根据post对象检查功能。此对象仅适用于已在DB中的帖子。因此,它主要用于允许/拒绝访问特定的帖子功能