• I am using the Twenty Twenty-one template in my WordPress site.

    I am trying to add a code snippet in my site so it shows a secondary menu only when the user logs in the system. I already created a secondary empty menu. I am using the following code snippet but it would not work:

    function add_login_logout_register_menu( $items, $args ) {
    	if ( $args->theme_location != 'secondary-menu' ) {
    		return $items;
    	}
    	if ( is_user_logged_in() ) {
    		$current_user = wp_get_current_user();
    		$items .= "Hola " . esc_html( $current_user->user_login ) . " | ";
    		$items .= '<a href="' . wp_logout_url() . '">CERRAR SESIÓN</a>';
    	} else {
    		$items .= '<a href="/login">INICIAR SESIÓN</a>';
    		$items .= '<a href="/registro">CREAR CUENTA</a>';
    	}
    	return $items;
    }
    add_filter( 'wp_nav_menu_items', 'add_login_logout_register_menu', 10, 2 );
    

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator threadi

    (@threadi)

    The secondary menu is called “footer” and not “secondary-menu”. Change

    if ( $args->theme_location != 'secondary-menu' ) {

    to

    if ( $args->theme_location != 'footer' ) {
    Thread Starter sdores

    (@sdores)

    Thank you, it worked. Is there a way to place this at the header of the page instead of placing down at the bottom?. So it is easier to spot it

    Moderator threadi

    (@threadi)

    For this you would have to overload the template via child theme. An introduction to child themes can be found here: https://developer.wordpress.org/themes/advanced-topics/child-themes/

    You would probably have to overload the footer.php file copied into the child theme, remove the menu inclusion there, and paste the exact code into the header.php file copied into the child theme at the point where you want to output it. After that you have to adjust the appearance via CSS.

    Important: do not adapt the original theme. Do this only via child theme.

    Thread Starter sdores

    (@sdores)

    Thank you so much

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Secondary menu’ is closed to new replies.