代码之家  ›  专栏  ›  技术社区  ›  DanielsV

php除非wordpress用户角色=“订阅者”显示此html代码

  •  0
  • DanielsV  · 技术社区  · 6 年前

    这是我无法使用的代码。

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

    附言 你可以看出,我不太擅长php。

    2 回复  |  直到 6 年前
        1
  •  0
  •   DanielsV    6 年前

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

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

    对于其他人,您可能需要更改“$this”部分。因此,您可以检索当前的_用户。也许有人仍然可以为他们张贴答案。

        2
  •  0
  •   Komal R    6 年前

    经过测试的代码。如果对你也有效,请告诉我!:)

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