How can I show the contents of only a few users

I’m trying to show the contents only for certain registered users. All users have the role of “agent”. To hide the contents of an unregistered user, I use this code:

<?php if  ( current_user_can( 'agent' )  ){ ?>
    ///here the content
<?php } ?>

Now I want to show the contents only for users with id 8,9, with this code:

Read More
<?php if  ( current_user_can( 'agent=8,9' )  ){ ?>
    ///here the content
<?php } ?>`,`<?php if  ( current_user_can( 'author_name=Jacks,David' )  ){ ?>
    ///here the contents
<?php } ?>

But nothing happens!!!
Please help.

Related posts

Leave a Reply

1 comment

  1. To show content only for users with role ‘agent’, try to use this snippet:

    <?php if ( ( $user = wp_get_current_user() ) && in_array( 'agent', $user->roles ) ) { ?>
        ///here the content
    <?php } ?>
    

    If you want to block users by ID, check the following snippet:

    <?php if ( ( $user = wp_get_current_user() ) && in_array( 'agent', $user->roles ) && in_array( $user->ID, array( 8, 9 ) ) ) { ?>
        ///here the content
    <?php } ?>