有很多方法可以进行基于级别的过滤——这取决于你想要做什么,但基本上你只需要简单的条件(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
内容加user 8
内容加 user 10
内容
放入简单的人类语言示例一将显示content for level 10 OR content for level 8 OR ..
,而示例 2 将显示content for level 10 AND content for level 8 AND
..
如前所述 - 有很多方法可以使用它,但这一切都取决于上下文。
注意:从 wp 3.0 开始,user_level 系统已被弃用。您将需要使用..按功能过滤user level to role Conversion system
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