Display content only for logged users

How can I show content only for logged user or admin in my template?

Depending on what you want to test, just add one of these snippets to your template:

Check if user is connected as admin

<?php
if( current_user_can('administrator')) {
echo "log1";
}
else echo "pas log1";
?>

Check if user is connected as admin, editor or author

<?php
$user = wp_get_current_user();
$allowed_roles = array('administrator','editor','author');
if( array_intersect($allowed_roles, $user->roles ) ) echo "log2";
else echo "pas log2";
?>

Check if user is connected

<?php
if ( is_user_logged_in() ) echo "log3";
else echo "pas log3";
?>

 

Submit a Comment

Your email address will not be published. Required fields are marked *