有很多方法可以进行基于级别的过滤 - 这取决于你想要做什么,但基本上你只需要简单的条件(if - else 或 if - then 或 switch 语句) - 取决于上下文.
if( current_user_can( 'level_10' ) ){
echo 'A - content for user level 10';
} elseif( current_user_can( 'level_8' ) ) {
echo 'B - content for user level 8 and above';
} elseif( current_user_can( 'level_6' ) ) {
echo 'C - content for user level 6 and above';
} // ok for wordpress < 3.0
/*
* Please read my note below regarding user levels to roles conversion
*/
if( current_user_can( 'manage_options' ) ){
echo 'A - content for user level Admin';
} elseif( current_user_can( 'publish_pages' ) ) {
echo 'B - content for user level Editor and above';
} elseif( current_user_can( 'publish_posts' ) ) {
echo 'C - content for user level Author and above';
} // ok for wordpress > 3.0
这将为每个用户输出完全不同的内容,但也意味着用户级别 10 将看不到级别 6 的内容..(除非您放弃 else..)
// when Admin logged
A - content for user level Admin
// when level editor and above logged
B - content for user level Editor and above
// when level author and above logged
C - content for user level Author and above
或
if( current_user_can( 'publish_posts' ) ){ // Author
echo 'A - content for Author and above';
if( current_user_can( 'publish_pages' ) ){ // Editor
echo 'B - Additional content for Editor and above';
if( current_user_can( 'manage_options' ) ){ // Administrator
echo 'C - Additional content for administrator ';
}
}
}
这将根据用户级别添加输出 - 所以用户 10 看到 user 6 content PLUS user 8 content PLUS user 10 content
用简单的人类语言示例一将显示content for level 10 OR content for level 8 OR ..,而示例二将显示content for level 10 AND content for level 8 AND ..
如前所述 - 有很多方法可以使用它,但这一切都取决于上下文。
注意:自 wp 3.0 起,user_level 系统已被弃用。您需要使用user level to role Conversion system 过滤Capabilities ..
if( current_user_can( 'administrator' ) ){} // only if administrator
if( current_user_can( 'editor' ) ){} // only if editor
if( current_user_can( 'author' ) ){} // only if author
if( current_user_can( 'contributor' ) ){} // only if contributor
if( current_user_can( 'subscriber' ) ){} // only if subscriber