【问题标题】:Different content for user level in WPWP中用户级别的不同内容
【发布时间】:2013-11-16 23:02:28
【问题描述】:

好的,所以我使用以下代码向 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 级等的用户显示不同的内容...

提前致谢

【问题讨论】:

  • 你到底想展示什么?例如,带有针对用户角色定制的链接的菜单,或按用户角色过滤的帖子页面?

标签: php wordpress members


【解决方案1】:

有很多方法可以进行基于级别的过滤 - 这取决于你想要做什么,但基本上你只需要简单的条件(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 的内容..(除非您放弃 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

【讨论】:

  • 感谢您的回复,但我需要一些额外的帮助。我想要为不同的用户级别显示不同文本的代码 E.G 级别 10 将看到带有管理仪表板链接的工作管理员,以便于访问。级别 9 会看到文本 PRO 级别 8 会看到 Advance,级别 7 会看到 FREE... 所以我想我需要你的第一个代码,但无法让它工作。是否有任何更改可以编辑它以包含&lt;?PHP ?&gt; 标签,这样我就可以复制和粘贴它,只需要编辑内容和级别。非常感谢
  • @Techyoucation - 对不起。既然您特别询问了user levels,我假设您知道您在问什么,并且您可能会使用旧版本(是的 - 有些人仍然这样做)。该系统现已弃用。请阅读我的更新和注意事项。
  • 您好,不,我使用的是最新版本的 WP,但使用了一个名为“restrict content pro”的插件,该插件使用级别...您提供的代码优先代码不起作用,但我可以使用它拧,因为我没有 PHP 知识,并在网上找到了代码。 - 谢谢 - 阿莱德
  • 我放置了您所使用的代码,但它仅用于级别 10,其他级别什么都没有显示?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 2011-06-18
  • 1970-01-01
  • 2020-11-03
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多