Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active February 29, 2016 06:37
Show Gist options
  • Save joshuadavidnelson/325d28ec621a7bf05658 to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/325d28ec621a7bf05658 to your computer and use it in GitHub Desktop.
Possible route to passing duplicate menu items in Menu Breadcrumbs through a filter for use in the theme. Per https://github.com/jchristopher/menu-breadcrumb/issues/7
<?php
/**
* Retrieve the current Menu item object for the current Menu.
*
* @since 1.0.0
* @return bool|WP_Post The current Menu item
*/
public function get_current_menu_item_object() {
$current_menu_item = false;
if ( empty( $this->menu_items ) ) {
return $current_menu_item;
}
// Setup an array to store all possible current menu items
$possible_menu_items = array();
// loop through the entire nav menu and determine whether any have a class="current" or are the current URL (e.g. a Custom Link was used)
foreach ( $this->menu_items as $menu_item ) {
// if WordPress was able to detect the current page
if ( is_array( $menu_item->classes ) && in_array( 'current', $menu_item->classes ) ) {
$current_menu_item = $menu_item;
}
// if the current URL matches a Custom Link
if ( ! $current_menu_item && isset( $menu_item->url ) && $this->is_at_url( $menu_item->url ) ) {
$current_menu_item = $menu_item;
}
// Add the current menu item to the array of possible menu items, reset and repeat
if( $current_menu_item ) {
$possible_menu_items[] = $current_menu_item;
$current_menu_item = false;
}
}
// Pull the first menu item by default, but filter it so others can be chosen.
if( isset( $possible_menu_items[0] ) )
$current_menu_item = apply_filters( 'menu_breadcrumb_current_menu_item', $possible_menu_items[0], $possible_menu_items );
return $current_menu_item;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment