Archived topic
Hide item on mobile when logged-in or logged-out
3 replies · Started by nik9 on December 7, 2020
Hello,
We use this function to add a icon in the navigation for mobile devices. However, is there a way to add a CSS class to hide this if a user is logged-in or logged-out?
add_action( 'generate_inside_mobile_header','my_account_mobile_menu' );
function my_account_mobile_menu() { ?>
<div class="mobile-bar-items">
<a title="My Account" href="/my-account/"><i class="fas fa-user"></i></a>
</div>
<?php }
Cheers
Hi,
You can modify your PHP snippet to use is_user_logged_in() on a if condition.
https://developer.wordpress.org/reference/functions/is_user_logged_in/
Example:
add_action( 'generate_inside_mobile_header','my_account_mobile_menu' );
function my_account_mobile_menu() {
if( is_user_logged_in() ) {
echo = '<div class="mobile-bar-items">
<a title="My Account" href="/my-account/"><i class="fas fa-user"></i></a>
</div>';
}
}
What this does is it only renders the markup on the hook if the user is logged in. Else, it doesn't do anything.
Hi Elvin, Thanks. But this snipped has a syntax error. Its not possible to save this without a error. :(
Ah my bad. I accidentally added = between echo and the markup string.
Here's the one w/o the error:
add_action( 'generate_inside_mobile_header','my_account_mobile_menu' );
function my_account_mobile_menu() {
if( is_user_logged_in() ) {
echo '<div class="mobile-bar-items">
<a title="My Account" href="/my-account/"><i class="fas fa-user"></i></a>
</div>';
}
}