【问题标题】:php unless wordpress user role = "subscriber" display this html codephp 除非 wordpress 用户角色 = "订阅者" 显示此 html 代码
【发布时间】:2019-05-06 10:21:37
【问题描述】:

我需要向除 wordpress 中的“订阅者”之外的所有用户角色显示 HTML 代码。

这是我无法工作的代码。

<?php
$current_user = wp_get_current_user();
?>

<?php if ( $current_user->role == 'subscriber' ) : ?>
    <span>here is my html</span>
<?php endif; ?>

PS 正如你所知道的那样,我对 php 不太擅长。

【问题讨论】:

    标签: php wordpress roles


    【解决方案1】:

    解决了!我使用了以下代码来提供所需的功能:

    <?php if ( $this->current_user_can_edit_posts ) : ?>
      <span>my html</span>
    <?php endif; ?>
    

    对于其他人,您可能需要更改“$this”部分。所以你可以检索 current_user。也许有人仍然可以为他们发布答案。

    【讨论】:

    • 哦,我看到你回答了自己的问题。伟大的!好的代码顺便说一句:)
    【解决方案2】:

    经过试验和测试的有效代码。让我知道它是否也适合你! :)

        <?php
          $current_user = wp_get_current_user();
          if ( ! in_array( 'subscriber', (array) $current_user->roles ) ) {
        ?>
          <span>here is my html</span>
        <?php
           } 
         ?>
    

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 2016-12-03
      • 2016-01-10
      相关资源
      最近更新 更多