Site logo

[Support request] Hide item on mobile when logged-in or logged-out

Home Forums Support [Support request] Hide item on mobile when logged-in or logged-out

Home Forums Support Hide item on mobile when logged-in or logged-out

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1570895
    nik9

    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

    #1570991
    Elvin
    Staff
    Customer Support

    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.

    #1571021
    nik9

    Hi Elvin, Thanks. But this snipped has a syntax error. Its not possible to save this without a error. 🙁

    #1571025
    Elvin
    Staff
    Customer Support

    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>';
        }
     }
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.