WP中用户级别的不同内容

时间:2013-11-16 23:02:28

标签: php wordpress members

好吧所以我使用以下代码向WordPress中具有不同级别的用户显示不同的内容

    <?php global $user_ID; if( $user_ID ) : ?>
<?php if( current_user_can('level_10') ) : ?>

<a href="http://techyoucation.com/wp-admin/">Admin</a>

<?php else : ?>

FREE

<?php endif; ?>
<?php endif; ?>

如何为10级,9级,8级,7级等用户展示不同的内容......

提前致谢

1 个答案:

答案 0 :(得分:0)

有很多方法可以进行基于级别的过滤 - 这取决于您想要做什么,但基本上您只需要简单的条件(if - elseif - thenswitch语句)再次 - 取决于背景。

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的内容..(除非你删除其他...)

// 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系统。您需要使用Capabilities ..

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
相关问题