Else help with user logged in/not logged in

Hi there,

I have the following code for a WordPress menu that displays content when a user is logged in:

  			<?php if ( is_user_logged_in() ): $current_user = wp_get_current_user(); ?>
  				<div class="user-profile-dropdown">
  					<a class="user-profile-name" href="#">
  						<div class="avatar">
  							<?php echo get_avatar( $current_user->ID ) ?>
  						</div>
                            <span>
                                <span class="welcome d-block">Welcome, <?php echo esc_html( $current_user->display_name ) ?></span>
                                My Account</span>
  						<?php if ( class_exists('WooCommerce') ): ?>
  							<div class="submenu-toggle"><i class="icons8-expand-arrow"></i></div>
  						<?php endif; ?>
  					</a>
  				</div>
  			<?php endif ?>

However, I would like to display some content for when the user is not logged in. I cannot work out how to do an else statement. on the above.

Can anyone help me in show some content when the user is not logged in/using an else statement?

Many thanks!

Not too hard…

<?php if ( is_user_logged_in() ): $current_user = wp_get_current_user(); ?>
  				<div class="user-profile-dropdown">
  					<a class="user-profile-name" href="#">
  						<div class="avatar">
  							<?php echo get_avatar( $current_user->ID ) ?>
  						</div>
                            <span>
                                <span class="welcome d-block">Welcome, <?php echo esc_html( $current_user->display_name ) ?></span>
                                My Account</span>
  						<?php if ( class_exists('WooCommerce') ): ?>
  							<div class="submenu-toggle"><i class="icons8-expand-arrow"></i></div>
  						<?php endif; ?>
  					</a>
  				</div>
  			<?php else: ?>
  			     <!-- Not logged in HTML and code here -->
  			<?php endif ?>

Notice where I put in the else. All your not logged in code can go in there.

1 Like

that’s fantastic, thank you for your help!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.