Skip to content

Instantly share code, notes, and snippets.

@diggeddy
Last active December 17, 2024 11:58
Show Gist options
  • Save diggeddy/d536a711522f37fd54e6118a18d80f26 to your computer and use it in GitHub Desktop.
Save diggeddy/d536a711522f37fd54e6118a18d80f26 to your computer and use it in GitHub Desktop.
Create a sub menu item container with action hook
<?php
add_filter( 'walker_nav_menu_start_el', 'db_sub_menu_item_hook', 10, 4 );
function db_sub_menu_item_hook( $item_output, $item, $depth, $args ) {
// Specify menu item class to target
$class_string = 'gp_mega_item';
$menu_item_classes = $item->classes;
if ( empty( $menu_item_classes ) || !in_array( $class_string , $menu_item_classes ) ) {
return $item_output;
}
$menu_item_slug = $item->post_name;
// create custom hook from $class_string and $menu_item_slug
// example result gp_mega_menu_{menu-item-name}
$custom_hook_name = $class_string . '_' . $menu_item_slug;
// Add our common and specific hooks
ob_start();
?>
<ul class="sub-menu custom-sub-menu">
<!-- <?php echo $custom_hook_name; ?> -->
<li>
<?php do_action('gp_before_mega_item'); ?>
<?php do_action($custom_hook_name); ?>
<?php do_action('gp_after_mega_item'); ?>
</li>
</ul>
<?php
$custom_sub_menu_html = ob_get_clean();
// return the output after the menu items anchor
$item_output .= $custom_sub_menu_html;
return $item_output;
}
@quantumleap33
Copy link

@DavidKeevis Thanks for providing the alternate CSS, Seems like your solution prevents other menu items from being selected while a submenu is open. I was also wondering if there was a way to set the sub menu full-width.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment