简体   繁体   中英

How Content Only to Logged in Users with Specific role users?

Till Now I found this Code, But this only works as logged in or not. I want to add a role filter too.

Anyone can help me in doing it?

<?php if ( is_user_logged_in() ) : ?>
<My Content>
<?php else : ?>
<Log in to Watch Content>

Here's your answer:

<?php   $user = wp_get_current_user();
    if ( in_array( 'admin', (array) $user->roles ) ) : ?>
       <Your Content>  
    <?php else : ?>
    <Log in to Watch Content>
<?php endif; ?>

You can do something like this. You do not need to do is_user_logged_in() Condition.

<?php
    $user = wp_get_current_user();
    if ( in_array( 'USER-ROLE', (array) $user->roles ) ) :
        // YOUR-CONTENT
    else :
        // YOUR-CONTENT
    endif;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM