0

I triy to add a ‘logout’ button on the main nav menu under ‘account’ parent menu. But, the button does not rendor.

At Appearance > Menus, I add ‘account’ page to the active menu. (link enabled) I add a code snippet The code here;

function my_insert_primary_nav_login_link( $items, $menu, $args ) { if( is_admin() ) return $items; // Bail out if this menu isn't main-menu or Main Menu is being loaded in the dashboard. if( $menu->slug !== 'main-menu' || is_admin() ) return $items; // Find the ID of the Page with the slug account in the menu items. foreach( $items as $item ) { if( $item->post_type !== 'page' || $item->post_name !== 'account' ) continue; $account_page_id = $item->ID; break; } // Bail if the account page is not in this menu. if( ! isset( $account_page_id ) ) return $items; $logged_in = is_user_logged_in(); $link_title = __( $logged_in ? 'Log In' : 'Log Out', 'oceanwp' ); // Insert a new WP_Post object for the link. $items[] = new WP_Post( (object) [ 'ID' => 'user-login', 'menu_item_parent' => $account_page_id, 'classes' => [ 'account-auth-link' ], 'type' => 'virtual', 'object' => 'virtual', 'title' => $link_title, 'attr_title' => $link_title, 'url' => $logged_in ? wp_logout_url() : wp_login_url(), ] ); return $items; } add_filter( 'wp_get_nav_menu_items', 'my_insert_primary_nav_login_link', 10, 3 ); 

I use OceanWP theme. I checked that the function fires and menu slug is main-menu but for some reason, it seemingly does not append 'account'parent menu.

enter image description here

enter image description here

enter image description here

If you could share your idea, I would much appreciated.

2
  • debug the loop on $items and edit your question to show us the result. Commented Feb 11 at 7:06
  • Hello mmm, I share the error log. [proxy_fcgi:error [pid [client: Got error 'PHP message: cgray_insert_primary_nav_login_link function called! PHP message: Checking item: Admin (Slug: 223, Type: post_type............ssage: Checking item: Workflow Inbox (Slug: 238, Type: post_type, Parent: 224)PHP message: Checking item: Workflow Status (Slug: 237, Type: post_type, Parent: 224)PHP message: Bailing out: Account page NOT found!', I think it goes over each items, but cannot append the 'account' page. I'm stuck wrapping around my head. Could you please share your idea? Commented Feb 13 at 20:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.