function detect_mob_child($parent, $echo = false) { $parent = $parent != "" ? $parent : '0'; $locations = get_nav_menu_locations(); $menu = wp_get_nav_menu_object($locations['primary']); $menu_items = wp_get_nav_menu_items($menu->term_id); _wp_menu_item_classes_by_context($menu_items); $menu_next = array(); foreach ((array) $menu_items as $key => $menu_item) { if ($menu_item->menu_item_parent == $parent) { $menu_next[(int) $menu_item->db_id] = $menu_item; } } unset($menu_items); if (!$echo) { if (!empty($menu_next)) { return true; } else { return false; } } else { $child_ul = '' . "\n"; $ret = ''; foreach ($menu_next as $i => $mnn) { //Specific additions add custom icons if ('page' == $mnn->object) { $post_finder = get_post($mnn->object_id); $page_slug = $post_finder->post_name; $newlink = strtolower(preg_replace('/[^a-zA-Z]/s', '', $page_slug)); $href = '#' . $newlink; $incl_onepage = get_post_meta($mnn->object_id, 'one_page', true); if ($incl_onepage == 1) { $href = '#' . $newlink; $identifyClass = "is_mob_onepage"; } else { $href = $mnn->url; $identifyClass = "not_mob_onepage"; } } else { $href = $mnn->url; $identifyClass = "not_mob_onepage"; } if (is_single() or is_page() or is_archive()) { $ret .= '<a class="' . $identifyClass . ' scroll-link" href="' . $href . '" data-soffset="0"> -' . $mnn->title . '</a>'; } else { //$return .= '<option class="'.$identifyClass.'" value="'. $href .'" onClick="'.$onchange_live.'">'. $men->title .'</option>'; $ret .= '<a class="' . $identifyClass . ' scroll-link" href="' . $href . '" data-soffset="0"> -' . $mnn->title . '</a>'; } //$ret .= '<li><a class="'.$identifyClass .'" href="'.$href.'">'.$mnn->title.'</a></li>' . "\n"; } unset($menu_next); $child_ul_close = '' . "\n"; if (!empty($ret)) { return $ret; } } }
function get_menu_items($theme_location) { $menu = get_menu($theme_location); $menu_items = array(); if ($menu) { $menu_items = wp_get_nav_menu_items($menu->term_id, array('update_post_term_cache' => false)); _wp_menu_item_classes_by_context($menu_items); } return $menu_items; }
function wordpress_boilerplate_find_menu_root($location, $selector, $directParent = false) { $locations = get_nav_menu_locations(); $menu = wp_get_nav_menu_object($locations[$location]); $menuItems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC', 'update_post_term_cache' => false)); _wp_menu_item_classes_by_context($menuItems); $sorted_menu_items = array(); foreach ((array) $menuItems as $menuItem) { $sorted_menu_items[$menuItem->menu_order] = $menuItem; } unset($menuItems); $sorted_menu_items = apply_filters('wp_nav_menu_objects', $sorted_menu_items, null); return wordpress_boilerplate_find_menu_root_from_items($sorted_menu_items, $selector, $directParent); }
/** * @param int $menu_id */ private function init($menu_id) { $menu = wp_get_nav_menu_items($menu_id); _wp_menu_item_classes_by_context($menu); if (is_array($menu)) { $menu = self::order_children($menu); } $this->items = $menu; $menu_info = wp_get_nav_menu_object($menu_id); $this->import($menu_info); $this->ID = $this->term_id; $this->id = $this->term_id; $this->title = $this->name; }
private function init_as_page_menu() { $menu = get_pages(); if ($menu) { foreach ($menu as $mi) { $mi->__title = $mi->post_title; } _wp_menu_item_classes_by_context($menu); if (is_array($menu)) { $menu = self::order_children($menu); } $this->items = $menu; } }
function order_children($items) { $index = array(); $menu = array(); _wp_menu_item_classes_by_context($items); foreach ($items as $item) { $index[$item->ID] = new TimberMenuItem($item); } foreach ($index as $item) { if ($item->menu_item_parent && isset($index[$item->menu_item_parent])) { $index[$item->menu_item_parent]->add_child($item); } else { $menu[] = $item; } } return $menu; }
function wp_nav_menu($args = array()) { static $menu_id_slugs = array(); $defaults = array('menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'depth' => 0, 'walker' => '', 'theme_location' => ''); $args = wp_parse_args($args, $defaults); $args = apply_filters('wp_nav_menu_args', $args); $args = (object) $args; $nav_menu = apply_filters('pre_wp_nav_menu', null, $args); // $nav_menu = null; if (null !== $nav_menu) { if ($args->echo) { echo $nav_menu; return; } return $nav_menu; } // Get the nav menu based on the requested menu // $menu = wp_get_nav_menu_object( $args->menu ); $menu = wp_get_nav_menu_object($args->menu_id); // Get the nav menu based on the theme_location // if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) ) // $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] ); if (!$menu && $args->theme_location) { $menu = wp_get_nav_menu_object($args->theme_location); } // get the first menu that has items if we still can't find a menu if (!$menu && !$args->theme_location) { $menus = wp_get_nav_menus(); foreach ($menus as $menu_maybe) { if ($menu_items = wp_get_nav_menu_items($menu_maybe->term_id, array('update_post_term_cache' => false))) { $menu = $menu_maybe; break; } } } if (empty($args->menu)) { $args->menu = $menu; } // If the menu exists, get its items. if ($menu && !is_wp_error($menu) && !isset($menu_items)) { $menu_items = wp_get_nav_menu_items($menu->term_id, array('update_post_term_cache' => false)); } $nav_menu = $items = ''; // $show_container = false; // if ( $args->container ) { // $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); // if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags ) ) { // $show_container = true; // $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"'; // $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : ''; // $nav_menu .= '<'. $args->container . $id . $class . '>'; // } // } // Set up the $menu_item variables _wp_menu_item_classes_by_context($menu_items); $sorted_menu_items = $menu_items_with_children = array(); foreach ((array) $menu_items as $menu_item) { $sorted_menu_items[$menu_item->menu_order] = $menu_item; if ($menu_item->menu_item_parent) { $menu_items_with_children[$menu_item->menu_item_parent] = true; } } // Add the menu-item-has-children class where applicable if ($menu_items_with_children) { foreach ($sorted_menu_items as &$menu_item) { if (isset($menu_items_with_children[$menu_item->ID])) { $menu_item->classes[] = 'menu-item-has-children'; } } } unset($menu_items, $menu_item); $sorted_menu_items = apply_filters('wp_nav_menu_objects', $sorted_menu_items, $args); $items .= walk_nav_menu_tree($sorted_menu_items, $args->depth, $args); unset($sorted_menu_items); // Attributes if (!empty($args->menu_id)) { $wrap_id = $args->menu_id; } else { $wrap_id = 'menu-' . $menu->slug; while (in_array($wrap_id, $menu_id_slugs)) { if (preg_match('#-(\\d+)$#', $wrap_id, $matches)) { $wrap_id = preg_replace('#-(\\d+)$#', '-' . ++$matches[1], $wrap_id); } else { $wrap_id = $wrap_id . '-1'; } } } $menu_id_slugs[] = $wrap_id; $wrap_class = $args->menu_class ? $args->menu_class : ''; $items = apply_filters('wp_nav_menu_items', $items, $args); $items = apply_filters("wp_nav_menu_{$menu->slug}_items", $items, $args); // Don't print any markup if there are no items at this point. if (empty($items)) { return false; } $nav_menu .= sprintf($args->items_wrap, esc_attr($wrap_id), esc_attr($wrap_class), $items); unset($items); if ($show_container) { $nav_menu .= '</' . $args->container . '>'; } $nav_menu = apply_filters('wp_nav_menu', $nav_menu, $args); if ($args->echo) { echo $nav_menu; } else { return $nav_menu; } }
/** * @ticket 35272 */ function test_class_not_applied_to_taxonomies_with_same_id_as_front_page_item() { global $wpdb; $new_id = 35272; $page_id = self::factory()->post->create(array('post_type' => 'page', 'post_title' => 'Home Page')); $tag_id = self::factory()->tag->create(); $wpdb->query("UPDATE {$wpdb->posts} SET ID={$new_id} WHERE ID={$page_id}"); $wpdb->query("UPDATE {$wpdb->terms} SET term_id={$new_id} WHERE term_id={$tag_id}"); $wpdb->query("UPDATE {$wpdb->term_taxonomy} SET term_id={$new_id} WHERE term_id={$tag_id}"); update_option('page_on_front', $new_id); wp_update_nav_menu_item($this->menu_id, 0, array('menu-item-type' => 'taxonomy', 'menu-item-object' => 'post_tag', 'menu-item-object-id' => $new_id, 'menu-item-status' => 'publish')); $menu_items = wp_get_nav_menu_items($this->menu_id); _wp_menu_item_classes_by_context($menu_items); $classes = $menu_items[0]->classes; $this->assertNotContains('menu-item-home', $classes); }
/** * Allows the Main Menu to combine with the Members Menu only when the users are logged in. * */ function members_menu($sorted_menu_items) { if (is_user_logged_in()) { $members_items = wp_get_nav_menu_items('Members Menu'); _wp_menu_item_classes_by_context($members_items); foreach ($members_items as $item) { $sorted_menu_items[] = $item; } } return $sorted_menu_items; }
/** * Return the breadcrumbs array * based on a given menu * * @param string $theme_location * @param array $options * * @return array|bool */ public static function get_breadcrumbs_arr($theme_location = 'primary', $options = array()) { $locations = get_nav_menu_locations(); if (!isset($locations[$theme_location])) { return false; } $options = wp_parse_args($options, array('home_title' => __('Home'))); $menu = wp_get_nav_menu_object($locations[$theme_location]); $items = wp_get_nav_menu_items($menu->term_id); if (empty($items)) { return array(); } // Set up the class variables, including current-classes _wp_menu_item_classes_by_context($items); $crumbs = array(); $current_exists = false; foreach ($items as $item) { if ($item->current_item_parent || $item->current_item_ancestor || $item->current) { $crumbs[] = self::get_breadcrumb_arr($item->url, $item->title, array('current' => $item->current, 'parent' => $item->current_item_parent, 'ancestor' => $item->current_item_ancestor)); if ($item->current) { $current_exists = true; } } } if (empty($crumbs)) { return array(); } if (!$current_exists) { array_push($crumbs, self::get_breadcrumb_arr(get_permalink(), get_the_title(), array('current' => true))); } if (!is_front_page()) { array_unshift($crumbs, self::get_breadcrumb_arr(home_url(), $options['home_title'], array('home_link' => true))); } return $crumbs; }
/** * @internal */ protected function init_as_page_menu() { $menu = get_pages(array('sort_column' => 'menu_order')); if ($menu) { foreach ($menu as $mi) { $mi->__title = $mi->post_title; } _wp_menu_item_classes_by_context($menu); if (is_array($menu)) { $menu = self::order_children($menu); } $this->items = $menu; } }
/** * @method menu * @staticvar array $menu_id_slugs * * @param {Array} $args * * @return boolean */ public static function menu($args = array()) { static $menu_id_slugs = array(); $defaults = array('menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'depth' => 0, 'walker' => '', 'theme_location' => ''); $args = wp_parse_args($args, $defaults); $args = apply_filters('wp_nav_menu_args', $args); $args = (object) $args; // Get the nav menu based on the requested menu // move get menu part to self::get_nav_menu_object function // to manage cache $menu = self::get_nav_menu_object($args); // If the menu exists, get its items. if ($menu && !is_wp_error($menu) && !isset($menu_items)) { $menu_items = self::get_nav_menu_items($menu->term_id, array('update_post_term_cache' => false)); } /* * If no menu was found: * - Fall back (if one was specified), or bail. * * If no menu items were found: * - Fall back, but only if no theme location was specified. * - Otherwise, bail. */ if ((!$menu || is_wp_error($menu) || isset($menu_items) && empty($menu_items) && !$args->theme_location) && $args->fallback_cb && is_callable($args->fallback_cb)) { return call_user_func($args->fallback_cb, (array) $args); } if (!$menu || is_wp_error($menu)) { return false; } $nav_menu = $items = ''; $show_container = false; if ($args->container) { $allowed_tags = apply_filters('wp_nav_menu_container_allowedtags', array('div', 'nav')); if (in_array($args->container, $allowed_tags)) { $show_container = true; $class = $args->container_class ? ' class="' . esc_attr($args->container_class) . '"' : ' class="menu-' . $menu->slug . '-container"'; $id = $args->container_id ? ' id="' . esc_attr($args->container_id) . '"' : ''; $nav_menu .= '<' . $args->container . $id . $class . '>'; } } // Set up the $menu_item variables _wp_menu_item_classes_by_context($menu_items); $sorted_menu_items = $menu_items_with_children = array(); foreach ((array) $menu_items as $key => $menu_item) { $sorted_menu_items[$menu_item->menu_order] = $menu_item; if ($menu_item->menu_item_parent) { $menu_items_with_children[$menu_item->menu_item_parent] = true; } } // Add the menu-item-has-children class where applicable if (!empty($menu_items_with_children)) { foreach ($sorted_menu_items as &$menu_item) { if (isset($menu_items_with_children[$menu_item->ID])) { $menu_item->classes[] = 'menu-item-has-children'; } } } unset($menu_items); $sorted_menu_items = apply_filters('wp_nav_menu_objects', $sorted_menu_items, $args); $items .= walk_nav_menu_tree($sorted_menu_items, $args->depth, $args); unset($sorted_menu_items); // Attributes if (!empty($args->menu_id)) { $wrap_id = $args->menu_id; } else { $wrap_id = 'menu-' . $menu->slug; while (in_array($wrap_id, $menu_id_slugs)) { if (preg_match('#-(\\d+)$#', $wrap_id, $matches)) { $wrap_id = preg_replace('#-(\\d+)$#', '-' . ++$matches[1], $wrap_id); } else { $wrap_id = $wrap_id . '-1'; } } } $menu_id_slugs[] = $wrap_id; $wrap_class = $args->menu_class ? $args->menu_class : ''; // Allow plugins to hook into the menu to add their own <li>'s $items = apply_filters('wp_nav_menu_items', $items, $args); $items = apply_filters("wp_nav_menu_{$menu->slug}_items", $items, $args); // Don't print any markup if there are no items at this point. if (empty($items)) { return false; } $nav_menu .= sprintf($args->items_wrap, esc_attr($wrap_id), esc_attr($wrap_class), $items); unset($items); if ($show_container) { $nav_menu .= '</' . $args->container . '>'; } $nav_menu = apply_filters('wp_nav_menu', $nav_menu, $args); if ($args->echo) { echo $nav_menu; } else { return $nav_menu; } }
protected function _render_menu_items($menu_items) { if (empty($menu_items)) { return ''; } _wp_menu_item_classes_by_context($menu_items); $children = array(); foreach ($menu_items as $item) { $pt = $item->menu_item_parent; $list = isset($children[$pt]) && !empty($children[$pt]) ? $children[$pt] : array(); array_push($list, $item); $children[$pt] = $list; } $lists = $this->menu_treerecurse(0, array(), $children); foreach ($lists as $item) { $item->title = esc_html($item->title); $item->level = $item->level + 1; $key = 'item-' . $item->ID; $setting = isset($this->settings[$key]) ? $this->settings[$key] : array(); // decode html tag if (isset($setting['caption']) && $setting['caption']) { $setting['caption'] = str_replace(array('[lt]', '[gt]'), array('<', '>'), $setting['caption']); } if ($item->level == 1 && isset($setting['caption']) && $setting['caption']) { $this->top_level_caption = true; } $item->class = ''; $item->mega = 0; $item->group = 0; $item->dropdown = 0; //$item->target = ''; if (isset($setting['group'])) { $item->group = 1; } else { if ($this->front_end) { if ($item->children && !isset($setting['hidesub']) || isset($setting['sub'])) { $item->dropdown = 1; } } else { if ($item->children || isset($setting['sub'])) { $item->dropdown = 1; } } } $item->mega = $item->group || $item->dropdown; if ($item->mega) { if (!isset($setting['sub'])) { $setting['sub'] = array(); } if ($item->children && (!isset($setting['sub']['rows']) || !count($setting['sub']['rows']))) { $c = $item->frist_children->ID; $setting['sub'] = array('rows' => array(array(array('width' => 12, 'item' => $c)))); } } $item->setting = $setting; $item->url = esc_url($item->url); $parent = isset($this->children[$item->menu_item_parent]) ? $this->children[$item->menu_item_parent] : array(); $parent[] = $item; $this->children[$item->menu_item_parent] = $parent; $this->items[$item->ID] = $item; } }
/** * insert languages list objects in nav menu at insertion point (filter wp_nav_menu_objects) * * @since 2.8.8 * @updated 2.9.11 (page) , 2.9.20 (menu) * @updated 2.10.1 - singular if exists * @updated 2.11.2 - better class assignation (ancestor) - thanks to Bastian * @updated 2.12.2 - compatible * */ function insert_language_objects_in_nav_menu($sorted_menu_items, $args) { global $post, $wp_query; // detect insertion point menu object and menu type $new_sorted_menu_items = array(); foreach ($sorted_menu_items as $key => $menu_object) { if ($menu_object->url == $this->insertion_point_dummy_link_menu) { // #insertmenu $queried_object = $wp_query->get_queried_object(); $queried_object_id = (int) $wp_query->queried_object_id; if (false !== strpos($menu_object->attr_title, 'menu-wo-')) { $langkey = explode('-', str_replace('menu-wo-', '', $menu_object->attr_title)); // approach < 2.14.2 } else { $langkey_ids = explode('-', str_replace('menu-wlid-', '', $menu_object->attr_title)); $langkey = array(); $id_slug = array_flip($this->langs_ids_array); foreach ($langkey_ids as $lang_id) { $langkey[] = $id_slug[$lang_id]; } } $menu_id_list = ''; $menu_slug_list = ''; foreach ($menu_object->classes as $one_class) { if (false !== strpos($one_class, 'xlmenulist-')) { $menu_id_list = str_replace('xlmenulist-', '', $one_class); continue; } else { if (false !== strpos($one_class, 'xlmenuslug')) { // to be compatible with export xml $menu_slug_list = str_replace('xlmenuslug' . $this->menu_slug_sep, '', $one_class); // -- seems better than _ continue; } } } if ($menu_id_list) { $menu_ids = explode('-', $menu_id_list); // here saved as term_id (container of menu items) (<2.12.2) } else { if ($menu_slug_list) { $menu_slugs = explode($this->menu_slug_sep, $menu_slug_list); foreach ($menu_slugs as $one_slug) { $term_data = term_exists($one_slug, 'nav_menu'); $menu_ids[] = is_array($term_data) ? $term_data['term_id'] : 0; } } } $menu_list = count($langkey) == count($menu_ids) ? array_combine($langkey, $menu_ids) : array(); // pb in count $curlang = the_curlang(); if (isset($menu_list[$curlang])) { $menu_structure_exists = term_exists((int) $menu_list[$curlang], 'nav_menu') ? true : false; } else { $menu_structure_exists = false; } if ($curlang && $menu_structure_exists) { $menu_items = wp_get_nav_menu_items($menu_list[$curlang]); // need term_id of structure _wp_menu_item_classes_by_context($menu_items); // added 2.11.2 if ($menu_items) { // added in 1.8 to sort inserted menu content and to insert class 'menu-item-has-children' as in nav-menu-template.php $sorted_menu_items = $menu_items_with_children = array(); foreach ((array) $menu_items as $menu_item) { $sorted_menu_items[$menu_item->menu_order] = $menu_item; if ($menu_item->menu_item_parent) { $menu_items_with_children[$menu_item->menu_item_parent] = true; } } // Add the menu-item-has-children class where applicable if ($menu_items_with_children) { foreach ($sorted_menu_items as &$menu_item) { if (isset($menu_items_with_children[$menu_item->ID])) { $menu_item->classes[] = 'menu-item-has-children'; } } } unset($menu_items, $menu_item); foreach ($sorted_menu_items as $new_menu_item) { // not recursive : impossible to decode insertion point inside menu if (!in_array($new_menu_item->url, array($this->insertion_point_dummy_link_menu, $this->insertion_point_dummy_link_page, $this->insertion_point_dummy_link))) { $new_classes = array("insertion-point"); if ($new_menu_item->menu_item_parent == 0) { $new_menu_item->menu_item_parent = $menu_object->menu_item_parent; } // heritate from insertion point $new_menu_item->classes = array_merge($menu_object->classes, $new_classes, $new_menu_item->classes); // fixed 2.11.2 $new_sorted_menu_items[] = $new_menu_item; } } } } } else { if ($menu_object->url == $this->insertion_point_dummy_link_page) { // #insertpagelist $classes = $menu_object->classes; $i = 0; $defaults = array('sort_order' => 'ASC', 'sort_column' => 'menu_order', 'hierarchical' => 1, QUETAG => $this->curlang); $r = wp_parse_args($menu_object->attr_title, $defaults); extract($r, EXTR_SKIP); $pagelist = get_pages($r); foreach ($pagelist as $onepage) { $class = is_page($onepage->ID) ? ' current-menu-item' : ''; $i++; $new_lang_menu_item = (object) array(); $id = $menu_object->ID * 1000 + $i; $new_lang_menu_item->ID = $id; $new_lang_menu_item->url = get_permalink($onepage->ID); // $onepage->guid; $new_lang_menu_item->title = $onepage->post_title; $new_lang_menu_item->attr_title = apply_filters('xl_nav_menu_page_attr_title', '...', $onepage->ID); $new_lang_menu_item->description = apply_filters('xl_nav_menu_page_description', '', $onepage->ID); // for twentyfifteen 2.15.4 $new_lang_menu_item->menu_item_parent = $menu_object->menu_item_parent; $new_lang_menu_item->db_id = $menu_object->db_id; $new_lang_menu_item->target = $menu_object->target; $new_lang_menu_item->classes = array_merge($menu_object->classes, explode(' ', $class)); $new_sorted_menu_items[] = $new_lang_menu_item; } } else { if ($menu_object->url == $this->insertion_point_dummy_link) { // language $classes = $menu_object->classes; $keys = array(); foreach ($this->langs_list_options as $one) { $keys[] = $one[0]; } $type_array = array_values(array_intersect($keys, $menu_object->classes)); $type = $type_array[0]; $hidden = true; // hidden here as defined in list - only available language are listed // create array of language menu objects $listlanguages = $this->get_listlanguages(); $new_menu_objects = array(); $i = 0; foreach ($listlanguages as $language) { $link = false; $display = $hidden && $this->xili_settings['lang_features'][$language->slug]['hidden'] == 'hidden' ? false : true; if ($display && !(in_array($type, array('navmenu-a', 'navmenu-1a', 'navmenu-1ao')) && $language->slug == the_curlang())) { $i++; if ($language->slug != the_curlang()) { $class = 'lang-' . $language->slug; } else { $class = 'lang-' . $language->slug . ' current-lang current-menu-item'; } $language_qv = $this->lang_slug_qv_trans($language->slug); if (in_array($type, array('navmenu-1', 'navmenu-1a', 'navmenu-1ao'))) { $this->doing_list_language = $language->slug; // for date filter if lang_perma $currenturl = $this->current_url($this->lang_perma); if (is_singular() && !is_front_page()) { if (in_array($type, array('navmenu-1a', 'navmenu-1'))) { // 2.13.3 $link = $this->link_of_linked_post($post->ID, $language->slug); } else { $targetpost = $this->linked_post_in($post->ID, $language->slug); if ($targetpost) { $link = get_permalink($targetpost); } } $title = sprintf(__($this->xili_settings['list_link_title']['current_post'], the_theme_domain()), __($language->description, $this->thetextdomain)); } else { if ($wp_query->is_posts_page) { // 2.8.4 $link = $this->link_of_linked_post(get_option('page_for_posts'), $language->slug); $title = sprintf(__($this->xili_settings['list_link_title']['latest_posts'], the_theme_domain()), _x($language->description, 'linktitle', $this->thetextdomain)); } else { if (function_exists('xili_tidy_tag_in_other_lang') && (is_tag() || $this->is_tax_improved())) { // 2.9.1 $q = ''; if (!is_tag() && $this->is_tax_improved()) { $queried_object = $wp_query->get_queried_object(); $q = '&tidy_post_tag=' . $queried_object->taxonomy; } if ($link = xili_tidy_tag_in_other_lang("format=term_link&lang=" . $language->name . $q)) { $title = xili_tidy_tag_in_other_lang("format=term_name&lang=" . $language->name . $q); } else { $link = $this->lang_perma ? str_replace('%lang%', $language_qv, $currenturl) : add_query_arg(array(QUETAG => $language_qv), $currenturl); $title = sprintf(__($this->xili_settings['list_link_title']['post_selected'], the_theme_domain()), _x($language->description, 'linktitle', $this->thetextdomain)); } } else { $link = $this->lang_perma ? str_replace('%lang%', $language_qv, $currenturl) : add_query_arg(array(QUETAG => $language_qv), $currenturl); $link = apply_filters('xiliml_language_list_menu_link', $link, $type, $language->slug, $language_qv); // 2.19.3 $title = sprintf(__($this->xili_settings['list_link_title']['post_selected'], the_theme_domain()), _x($language->description, 'linktitle', $this->thetextdomain)); } } } $this->doing_list_language = false; } else { // 'navmenu', 'navmenu-a' $link = $this->lang_perma ? str_replace('%lang%', $language_qv, get_bloginfo('url') . '/%lang%/') : add_query_arg(array(QUETAG => $language_qv), get_bloginfo('url')); $title = esc_attr(sprintf(__($this->xili_settings['list_link_title']['post_selected'], $this->thetextdomain), _x($language->description, 'linktitle', $this->thetextdomain))); } // only required values... if ($link) { $new_lang_menu_item = (object) array(); $id = $menu_object->ID * 100 + $i; $new_lang_menu_item->ID = $id; $new_lang_menu_item->url = $link; $new_lang_menu_item->title = __($language->description, $this->thetextdomain); $new_lang_menu_item->attr_title = $title; $new_lang_menu_item->description = apply_filters('xl_nav_menu_lang_description', '', $language->slug); // for twentyfifteen 2.15.4 $new_lang_menu_item->menu_item_parent = $menu_object->menu_item_parent; $new_lang_menu_item->db_id = $menu_object->db_id; $new_lang_menu_item->target = $menu_object->target; $new_lang_menu_item->classes = array_merge($menu_object->classes, explode(' ', $class)); $new_sorted_menu_items[] = $new_lang_menu_item; } } // language } // display } else { // no dummy insertion $new_sorted_menu_items[] = $menu_object; } } } } // foreach menu return $new_sorted_menu_items; }
<div class="rockharbor-logo"> <a href="<?php echo get_site_url(); ?> "> <?php echo $theme->Html->image('logo_1.png', array('alt' => 'ROCKHARBOR', 'parent' => false)); ?> </a> </div> <nav id="access" role="navigation" class="clearfix"> <?php $menu_items = wp_get_nav_menu_items($locations['main'], array('auto_show_children' => true)); _wp_menu_item_classes_by_context($menu_items); $menu = array(); $ids = array(); foreach ($menu_items as $key => $menu_item) { $a = $theme->Html->tag('a', $menu_item->title, array('href' => $menu_item->url)); $opts = array('class' => implode(' ', $menu_item->classes)); if ($menu_item->menu_item_parent == 0) { // top level $menu[] = array('a' => $a, 'opts' => $opts, 'children' => array()); $ids[$menu_item->ID] = count($menu) - 1; } else { // child $menu[$ids[$menu_item->menu_item_parent]]['children'][] = $theme->Html->tag('li', $a, $opts); } } $output = '';
function renova_detect_child($parent, $echo = false) { $parent = $parent != "" ? $parent : '0'; $locations = get_nav_menu_locations(); $menu = wp_get_nav_menu_object($locations['primary']); $menu_items = wp_get_nav_menu_items($menu->term_id); _wp_menu_item_classes_by_context($menu_items); $menu_next = array(); foreach ((array) $menu_items as $key => $menu_item) { if ($menu_item->menu_item_parent == $parent) { $menu_next[(int) $menu_item->db_id] = $menu_item; } } unset($menu_items); if (!$echo) { if (!empty($menu_next)) { return true; } else { return false; } } else { $child_ul = '<ul>' . "\n"; $ret = ''; foreach ($menu_next as $i => $mnn) { //Specific additions add custom icons if ('page' == $mnn->object) { $post_finder = get_post($men->object_id); $page_slug = $post_finder->post_name; $newlink = strtolower(preg_replace('/[^a-zA-Z]/s', '', $page_slug)); $href = '#' . $newlink; $iconp = get_post_meta($mnn->object_id, 'menu_icon', true); $incl_onepage = get_post_meta($mnn->object_id, 'one_page', true); if ($incl_onepage == 'yes' or $incl_onepage == 'Yes') { $href = '#' . $newlink; $identifyClass = "is_onepage"; } else { $href = $mnn->url; $identifyClass = "not_onepage"; } } else { $href = $mnn->url; $identifyClass = "not_onepage"; } $ret .= '<li><a class="scroll-link ' . $identifyClass . '" href="' . $href . '" id="' . $newlink . '-linker" data-soffset="100">' . $mnn->title . '</a></li>' . "\n"; } unset($menu_next); $child_ul_close = '</ul>' . "\n"; if (!empty($ret)) { return $child_ul . $ret . $child_ul_close; } } }
function custom_mobile_menu() { $locations = get_nav_menu_locations(); if (!isset($locations['primary'])) { $return = '<ul class="clearfix"> <li>Please configure the menu navigation</li></ul>'; } else { $menu = wp_get_nav_menu_object($locations['primary']); $return = ''; if (empty($menu)) { $return = '<ul class="clearfix"> <li>Please configure the menu navigation</li></ul>'; } else { $menu_items = wp_get_nav_menu_items($menu->term_id); _wp_menu_item_classes_by_context($menu_items); $return = '<ul id="mob-nav" class="clearfix">' . "\n"; $menunu = array(); foreach ((array) $menu_items as $key => $menu_item) { $menunu[(int) $menu_item->db_id] = $menu_item; } unset($menu_items); foreach ($menunu as $i => $men) { if ($men->menu_item_parent == '0') { //Specific additions if ('page' == $men->object) { $incl_onepage = get_post_meta($men->object_id, 'one_page', true); $small_title = strtolower(preg_replace('/\\s+/', '-', $men->post_excerpt)); if ($incl_onepage == 'yes' or $incl_onepage == 'Yes') { $href = '#' . $small_title; $identifyClass = "scroll-link is_onepage"; } else { $href = $men->url; $identifyClass = "not_onepage"; } } else { $href = $men->url; $identifyClass = "not_onepage"; $small_title = strtolower(preg_replace('/\\s+/', '-', $men->title)); } $return .= '<li>'; $return .= '<a href="' . $href . '" class="' . $identifyClass . '" data-soffset="0">' . $men->title . '</a>'; $return .= '</li>' . "\n"; $has_sub_menu = 0; foreach ($menunu as $submenu) { if ($submenu->menu_item_parent == $men->ID) { $has_sub_menu = 1; } } if ($has_sub_menu == 1) { foreach ($menunu as $submenu) { if ($submenu->menu_item_parent == $men->ID) { $return .= '<li>'; if ('page' == $submenu->object) { $incl_onepage = get_post_meta($submenu->object_id, 'one_page', true); $small_title = strtolower(preg_replace('/\\s+/', '-', $submenu->post_excerpt)); if ($incl_onepage == 'yes' or $incl_onepage == 'Yes') { $href = '#' . $small_title; $identifyClass = "scroll-link is_onepage"; } else { $href = $submenu->url; $identifyClass = "not_onepage"; } } else { $href = $submenu->url; $identifyClass = "not_onepage"; $small_title = strtolower(preg_replace('/\\s+/', '-', $submenu->title)); } $return .= '<a href="' . $href . '" class="' . $identifyClass . '">' . $submenu->title . '</a>'; $return .= '</li>' . "\n"; } } } } } unset($menunu); $return .= '</ul>' . "\n"; } } echo $return; }
function cpcm_replace_taxonomy_by_posts($sorted_menu_items, $args) { $this->getOptions(); $result = array(); $inc = 0; $menu_item_parent_map = array(); // Holds, for each menu item I that was removed, a link to the item that should become the new parent P of menu items under I foreach ((array) $sorted_menu_items as $key => $menu_item) { $menu_item->menu_order = $menu_item->menu_order + $inc; // Augment taxonomy object with a list of its posts: Append posts to $result // Optional: Remove the taxonomy object/original menu item itself. if ($menu_item->type == 'taxonomy' && get_post_meta($menu_item->db_id, "_cpcm-unfold", true) == '1') { $query_arr = array(); // Example: Array ( [0] => Array ( [taxonomy] => category [field] => id [terms] => 3 ) ), i.e. get a category by id, where id = 3 $query_arr['tax_query'] = array('relation' => 'AND', array('taxonomy' => $menu_item->object, 'field' => 'id', 'terms' => $menu_item->object_id)); $subcategory_behavior = get_post_meta($menu_item->db_id, "_cpcm-subcategories", true); switch ($subcategory_behavior) { case "exclude": // Subcategories (subtaxonomies) should be excluded, so append a query to tax_query that does exactly that $category_children = array_diff(get_term_children($menu_item->object_id, $menu_item->object), array("")); if (!empty($category_children)) { $query_arr['tax_query'][] = array('taxonomy' => $menu_item->object, 'terms' => $category_children, 'field' => 'id', 'operator' => 'NOT IN'); } break; case "flatten": /* No additional filtering */ /* No additional filtering */ default: break; } // If _cpcm-unfold is true, the following custom fields exist: $query_arr['order'] = get_post_meta($menu_item->db_id, "_cpcm-order", true); $query_arr['orderby'] = get_post_meta($menu_item->db_id, "_cpcm-orderby", true); $query_arr['numberposts'] = get_post_meta($menu_item->db_id, "_cpcm-item-count", true); // default value of -1 returns all posts $query_arr['offset'] = get_post_meta($menu_item->db_id, "_cpcm-item-skip", true); // default value of 0 skips no posts $query_arr['posts_per_page'] = $query_arr['numberposts'] != '-1' ? get_post_meta($menu_item->db_id, "_cpcm-item-count", true) : 100000; // http://wordpress.stackexchange.com/a/13376 posts_per_page does not accept -1, but is a required argument to make offset work. So, whenever numberposts is positive, use that. Otherwise, use the proposed workaround on the SO topic. // Support for custom post types $tag = get_taxonomy($menu_item->object); $query_arr['post_type'] = $tag->object_type; // Allow plugin extensions that further modify the query if (has_filter('cpcm_filter_posts_query')) { $query_arr = apply_filters('cpcm_filter_posts_query', $query_arr, $menu_item); } $posts = get_posts($query_arr); // Decide whether the original item needs to be preserved. $remove_original_item = get_post_meta($menu_item->db_id, "_cpcm-remove-original-item", true); $menu_item_parent = $menu_item->menu_item_parent; switch ($remove_original_item) { case "always": if (empty($posts)) { $inc -= 1; $menu_item_parent_map[$menu_item->db_id] = $menu_item->menu_item_parent; } else { if (count($posts) == 1) { // If the menu-item should be removed, but it has exactly one post, then use this post as new parent for any menu items down the line. // Because we can't use posts as menu items (they don't have a db_id), reuse the menu_item object and transfer the post properties to the menu_item in the foreach loop // See {note 1} in foreach array_push($result, $menu_item); } else { $inc -= 1; $menu_item_parent_map[$menu_item->db_id] = $menu_item->menu_item_parent; } } break; case "only if empty": if (empty($posts)) { $inc -= 1; $menu_item_parent_map[$menu_item->db_id] = $menu_item->menu_item_parent; } else { array_push($result, $menu_item); } break; case "never": array_push($result, $menu_item); break; } if (is_numeric($query_arr['numberposts']) && $query_arr['numberposts'] == '0') { continue; } // Set the menu_item_parent for the menu_item: If the parent item was removed, go up a level $current_parent_id = $menu_item->menu_item_parent; while (array_key_exists(strval($current_parent_id), $menu_item_parent_map) == 1) { $current_parent_id = $menu_item_parent_map[$current_parent_id]; } $menu_item->menu_item_parent = $current_parent_id; foreach ((array) $posts as $pkey => $post) { $post = wp_setup_nav_menu_item($post); // Set the menu_item_parent for the post: If the parent item was removed, go up a level $current_parent_id = $menu_item->db_id; while (array_key_exists(strval($current_parent_id), $menu_item_parent_map) == 1) { $current_parent_id = $menu_item_parent_map[$current_parent_id]; } $post->menu_item_parent = $current_parent_id; // Transfer properties from the old menu item to the new one $post->target = $menu_item->target; //$post->classes = $menu_item->classes; // Don't copy the classes, because this will also copy the 'active' CSS class too all siblings of the selected menu item. http://wordpress.org/support/topic/active-css-class $post->classes = array_merge($post->classes, (array) get_post_meta($menu_item->db_id, "_menu_item_classes", true)); // copy custom css classes that the user specified under "CSS Classes (optional)" $post->xfn = $menu_item->xfn; $post->description = $menu_item->description; // Set the title of the new menu item $post->title = get_post_meta($menu_item->db_id, "_cpcm-item-titles", true); // Replace the placeholders in the title by the properties of the post $post->title = $this->replace_placeholders($post, $post->title); $inc += 1; $post->menu_order = $menu_item->menu_order + $inc; } // Solve https://wordpress.org/support/topic/works-with-41-as-far-as-i-can-tell?replies=5, regenerate all classes for the posts, and copy those classes to the menu_item that we're reusing. // Extend the items with classes. _wp_menu_item_classes_by_context($posts); // Decorate the posts with the required data for a menu-item. if (count($posts) == 1 && $remove_original_item == "always") { // {note 1} // Do not use the post, but re-use the menu item instead. $menu_item->title = get_post_meta($menu_item->db_id, "_cpcm-item-titles", true); $menu_item->title = $this->replace_placeholders($post, $menu_item->title); $menu_item->url = get_permalink($post->ID); $menu_item->classes = $posts[0]->classes; } else { // Append the new menu_items to the menu array that we're building. $result = array_merge($result, $posts); } } else { // Other objects may have a parent that has been removed by cpcm. Fix that here. // Set the menu_item_parent for the menu_item: If the parent item was removed, go up a level $current_parent_id = $menu_item->menu_item_parent; while (array_key_exists(strval($current_parent_id), $menu_item_parent_map) == 1) { $current_parent_id = $menu_item_parent_map[$current_parent_id]; } $menu_item->menu_item_parent = $current_parent_id; // Treat other objects as usual, but note that the position // of elements in the array changes. array_push($result, $menu_item); } } unset($sorted_menu_items); unset($menu_item_parent_map); // Apply _wp_menu_item_classes_by_context not only to the $posts array, but to the whole result array so that the classes for the original menu items are regenerated as well. Solves: http://wordpress.org/support/topic/issue-with-default-wordpress-sidebar-menu and http://wordpress.org/support/topic/menu-do-not-include-the-current-menu-parent-class _wp_menu_item_classes_by_context($result); return $result; }
/** * Displays a navigation menu. * * Optional $args contents: * * menu - The menu that is desired. Accepts (matching in order) id, slug, name. Defaults to blank. * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'. * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented. * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'. * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'. * container_id - The ID that is applied to the container. Defaults to blank. * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'. Set to false for no fallback. * before - Text before the link text. * after - Text after the link text. * link_before - Text before the link. * link_after - Text after the link. * echo - Whether to echo the menu or return it. Defaults to echo. * depth - how many levels of the hierarchy are to be included. 0 means all. Defaults to 0. * walker - allows a custom walker to be specified. * theme_location - the location in the theme to be used. Must be registered with register_nav_menu() in order to be selectable by the user. * items_wrap - How the list items should be wrapped. Defaults to a ul with an id and class. Uses printf() format with numbered placeholders. * * @since 3.0.0 * * @param array $args Arguments */ function wp_nav_menu($args = array()) { static $menu_id_slugs = array(); $defaults = array('menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'depth' => 0, 'walker' => '', 'theme_location' => ''); $args = wp_parse_args($args, $defaults); $args = apply_filters('wp_nav_menu_args', $args); $args = (object) $args; // Get the nav menu based on the requested menu $menu = wp_get_nav_menu_object($args->menu); // Get the nav menu based on the theme_location if (!$menu && $args->theme_location && ($locations = get_nav_menu_locations()) && isset($locations[$args->theme_location])) { $menu = wp_get_nav_menu_object($locations[$args->theme_location]); } // get the first menu that has items if we still can't find a menu if (!$menu && !$args->theme_location) { $menus = wp_get_nav_menus(); foreach ($menus as $menu_maybe) { if ($menu_items = wp_get_nav_menu_items($menu_maybe->term_id)) { $menu = $menu_maybe; break; } } } // If the menu exists, get its items. if ($menu && !is_wp_error($menu) && !isset($menu_items)) { $menu_items = wp_get_nav_menu_items($menu->term_id); } // If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists if ((!$menu || is_wp_error($menu) || isset($menu_items) && empty($menu_items) && !$args->theme_location) && $args->fallback_cb && is_callable($args->fallback_cb)) { return call_user_func($args->fallback_cb, (array) $args); } // If no fallback function was specified and the menu doesn't exists, bail. if (!$menu || is_wp_error($menu)) { return false; } $nav_menu = $items = ''; $show_container = false; if ($args->container) { $allowed_tags = apply_filters('wp_nav_menu_container_allowedtags', array('div', 'nav')); if (in_array($args->container, $allowed_tags)) { $show_container = true; $class = $args->container_class ? ' class="' . esc_attr($args->container_class) . '"' : ' class="menu-' . $menu->slug . '-container"'; $id = $args->container_id ? ' id="' . esc_attr($args->container_id) . '"' : ''; $nav_menu .= '<' . $args->container . $id . $class . '>'; } } // Set up the $menu_item variables _wp_menu_item_classes_by_context($menu_items); $sorted_menu_items = array(); foreach ((array) $menu_items as $key => $menu_item) { $sorted_menu_items[$menu_item->menu_order] = $menu_item; } unset($menu_items); $sorted_menu_items = apply_filters('wp_nav_menu_objects', $sorted_menu_items, $args); $items .= walk_nav_menu_tree($sorted_menu_items, $args->depth, $args); unset($sorted_menu_items); // Attributes if (!empty($args->menu_id)) { $wrap_id = $args->menu_id; } else { $wrap_id = 'menu-' . $menu->slug; while (in_array($wrap_id, $menu_id_slugs)) { if (preg_match('#-(\\d+)$#', $wrap_id, $matches)) { $wrap_id = preg_replace('#-(\\d+)$#', '-' . ++$matches[1], $wrap_id); } else { $wrap_id = $wrap_id . '-1'; } } } $menu_id_slugs[] = $wrap_id; $wrap_class = $args->menu_class ? $args->menu_class : ''; // Allow plugins to hook into the menu to add their own <li>'s $items = apply_filters('wp_nav_menu_items', $items, $args); $items = apply_filters("wp_nav_menu_{$menu->slug}_items", $items, $args); $nav_menu .= sprintf($args->items_wrap, esc_attr($wrap_id), esc_attr($wrap_class), $items); unset($items); if ($show_container) { $nav_menu .= '</' . $args->container . '>'; } $nav_menu = apply_filters('wp_nav_menu', $nav_menu, $args); if ($args->echo) { echo $nav_menu; } else { return $nav_menu; } }
/** * Displays a navigation menu. * * @since 3.0.0 * * @param array $args { * Optional. Array of nav menu arguments. * * @type string $menu Desired menu. Accepts (matching in order) id, slug, name. Default empty. * @type string $menu_class CSS class to use for the ul element which forms the menu. Default 'menu'. * @type string $menu_id The ID that is applied to the ul element which forms the menu. * Default is the menu slug, incremented. * @type string $container Whether to wrap the ul, and what to wrap it with. Default 'div'. * @type string $container_class Class that is applied to the container. Default 'menu-{menu slug}-container'. * @type string $container_id The ID that is applied to the container. Default empty. * @type callback|bool $fallback_cb If the menu doesn't exists, a callback function will fire. * Default is 'wp_page_menu'. Set to false for no fallback. * @type string $before Text before the link text. Default empty. * @type string $after Text after the link text. Default empty. * @type string $link_before Text before the link. Default empty. * @type string $link_after Text after the link. Default empty. * @type bool $echo Whether to echo the menu or return it. Default true. * @type int $depth How many levels of the hierarchy are to be included. 0 means all. Default 0. * @type object $walker Instance of a custom walker class. Default empty. * @type string $theme_location Theme location to be used. Must be registered with register_nav_menu() * in order to be selectable by the user. * @type string $items_wrap How the list items should be wrapped. Default is a ul with an id and class. * Uses printf() format with numbered placeholders. * } * @return mixed Menu output if $echo is false, false if there are no items or no menu was found. */ function wp_nav_menu($args = array()) { static $menu_id_slugs = array(); $defaults = array('menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'depth' => 0, 'walker' => '', 'theme_location' => ''); $args = wp_parse_args($args, $defaults); /** * Filter the arguments used to display a navigation menu. * * @since 3.0.0 * * @see wp_nav_menu() * * @param array $args Array of wp_nav_menu() arguments. */ $args = apply_filters('wp_nav_menu_args', $args); $args = (object) $args; /** * Filter whether to short-circuit the wp_nav_menu() output. * * Returning a non-null value to the filter will short-circuit * wp_nav_menu(), echoing that value if $args->echo is true, * returning that value otherwise. * * @since 3.9.0 * * @see wp_nav_menu() * * @param string|null $output Nav menu output to short-circuit with. Default null. * @param object $args An object containing wp_nav_menu() arguments. */ $nav_menu = apply_filters('pre_wp_nav_menu', null, $args); if (null !== $nav_menu) { if ($args->echo) { echo $nav_menu; return; } return $nav_menu; } // Get the nav menu based on the requested menu $menu = wp_get_nav_menu_object($args->menu); // Get the nav menu based on the theme_location if (!$menu && $args->theme_location && ($locations = get_nav_menu_locations()) && isset($locations[$args->theme_location])) { $menu = wp_get_nav_menu_object($locations[$args->theme_location]); } // get the first menu that has items if we still can't find a menu if (!$menu && !$args->theme_location) { $menus = wp_get_nav_menus(); foreach ($menus as $menu_maybe) { if ($menu_items = wp_get_nav_menu_items($menu_maybe->term_id, array('update_post_term_cache' => false))) { $menu = $menu_maybe; break; } } } // If the menu exists, get its items. if ($menu && !is_wp_error($menu) && !isset($menu_items)) { $menu_items = wp_get_nav_menu_items($menu->term_id, array('update_post_term_cache' => false)); } /* * If no menu was found: * - Fall back (if one was specified), or bail. * * If no menu items were found: * - Fall back, but only if no theme location was specified. * - Otherwise, bail. */ if ((!$menu || is_wp_error($menu) || isset($menu_items) && empty($menu_items) && !$args->theme_location) && $args->fallback_cb && is_callable($args->fallback_cb)) { return call_user_func($args->fallback_cb, (array) $args); } if (!$menu || is_wp_error($menu)) { return false; } $nav_menu = $items = ''; $show_container = false; if ($args->container) { /** * Filter the list of HTML tags that are valid for use as menu containers. * * @since 3.0.0 * * @param array $tags The acceptable HTML tags for use as menu containers. * Default is array containing 'div' and 'nav'. */ $allowed_tags = apply_filters('wp_nav_menu_container_allowedtags', array('div', 'nav')); if (in_array($args->container, $allowed_tags)) { $show_container = true; $class = $args->container_class ? ' class="' . esc_attr($args->container_class) . '"' : ' class="menu-' . $menu->slug . '-container"'; $id = $args->container_id ? ' id="' . esc_attr($args->container_id) . '"' : ''; $nav_menu .= '<' . $args->container . $id . $class . '>'; } } // Set up the $menu_item variables _wp_menu_item_classes_by_context($menu_items); $sorted_menu_items = $menu_items_with_children = array(); foreach ((array) $menu_items as $menu_item) { $sorted_menu_items[$menu_item->menu_order] = $menu_item; if ($menu_item->menu_item_parent) { $menu_items_with_children[$menu_item->menu_item_parent] = true; } } // Add the menu-item-has-children class where applicable if ($menu_items_with_children) { foreach ($sorted_menu_items as &$menu_item) { if (isset($menu_items_with_children[$menu_item->ID])) { $menu_item->classes[] = 'menu-item-has-children'; } } } unset($menu_items, $menu_item); /** * Filter the sorted list of menu item objects before generating the menu's HTML. * * @since 3.1.0 * * @param array $sorted_menu_items The menu items, sorted by each menu item's menu order. * @param object $args An object containing wp_nav_menu() arguments. */ $sorted_menu_items = apply_filters('wp_nav_menu_objects', $sorted_menu_items, $args); $items .= walk_nav_menu_tree($sorted_menu_items, $args->depth, $args); unset($sorted_menu_items); // Attributes if (!empty($args->menu_id)) { $wrap_id = $args->menu_id; } else { $wrap_id = 'menu-' . $menu->slug; while (in_array($wrap_id, $menu_id_slugs)) { if (preg_match('#-(\\d+)$#', $wrap_id, $matches)) { $wrap_id = preg_replace('#-(\\d+)$#', '-' . ++$matches[1], $wrap_id); } else { $wrap_id = $wrap_id . '-1'; } } } $menu_id_slugs[] = $wrap_id; $wrap_class = $args->menu_class ? $args->menu_class : ''; /** * Filter the HTML list content for navigation menus. * * @since 3.0.0 * * @see wp_nav_menu() * * @param string $items The HTML list content for the menu items. * @param object $args An object containing wp_nav_menu() arguments. */ $items = apply_filters('wp_nav_menu_items', $items, $args); /** * Filter the HTML list content for a specific navigation menu. * * @since 3.0.0 * * @see wp_nav_menu() * * @param string $items The HTML list content for the menu items. * @param object $args An object containing wp_nav_menu() arguments. */ $items = apply_filters("wp_nav_menu_{$menu->slug}_items", $items, $args); // Don't print any markup if there are no items at this point. if (empty($items)) { return false; } $nav_menu .= sprintf($args->items_wrap, esc_attr($wrap_id), esc_attr($wrap_class), $items); unset($items); if ($show_container) { $nav_menu .= '</' . $args->container . '>'; } /** * Filter the HTML content for navigation menus. * * @since 3.0.0 * * @see wp_nav_menu() * * @param string $nav_menu The HTML content for the navigation menu. * @param object $args An object containing wp_nav_menu() arguments. */ $nav_menu = apply_filters('wp_nav_menu', $nav_menu, $args); if ($args->echo) { echo $nav_menu; } else { return $nav_menu; } }
/** * Alternative output for wp_nav_menu for the 'social' menu location. * * @since 1.0.0. * * @param string $output Output for the menu. * @param object $args wp_nav_menu arguments. * @return string Modified menu. */ function ttfmake_pre_wp_nav_menu_social($output, $args) { if (!$args->theme_location || 'social' !== $args->theme_location) { return $output; } // Get the menu object $locations = get_nav_menu_locations(); $menu = wp_get_nav_menu_object($locations[$args->theme_location]); if (!$menu || is_wp_error($menu)) { return $output; } $output = ''; // Get the menu items $menu_items = wp_get_nav_menu_items($menu->term_id, array('update_post_term_cache' => false)); // Set up the $menu_item variables _wp_menu_item_classes_by_context($menu_items); // Sort the menu items $sorted_menu_items = array(); foreach ((array) $menu_items as $menu_item) { $sorted_menu_items[$menu_item->menu_order] = $menu_item; } unset($menu_items, $menu_item); /** * Filter the supported social icons. * * This array uses the url pattern for the key and the CSS class (as dictated by Font Awesome) as the array value. * The URL pattern is used to match the URL used by a menu item. * * @since 1.2.3. * * @param array $icons The array of supported social icons. */ $supported_icons = apply_filters('make_supported_social_icons', array('500px.com' => 'fa-500px', 'amazon.com' => 'fa-amazon', 'angel.co' => 'fa-angellist', 'app.net' => 'fa-adn', 'behance.net' => 'fa-behance', 'bitbucket.org' => 'fa-bitbucket', 'codepen.io' => 'fa-codepen', 'delicious.com' => 'fa-delicious', 'deviantart.com' => 'fa-deviantart', 'digg.com' => 'fa-digg', 'dribbble.com' => 'fa-dribbble', 'facebook.com' => 'fa-facebook-official', 'flickr.com' => 'fa-flickr', 'foursquare.com' => 'fa-foursquare', 'github.com' => 'fa-github', 'gittip.com' => 'fa-gittip', 'plus.google.com' => 'fa-google-plus-square', 'houzz.com' => 'fa-houzz', 'instagram.com' => 'fa-instagram', 'jsfiddle.net' => 'fa-jsfiddle', 'last.fm' => 'fa-lastfm', 'leanpub.com' => 'fa-leanpub', 'linkedin.com' => 'fa-linkedin', 'medium.com' => 'fa-medium', 'ok.ru' => 'fa-odnoklassniki', 'pinterest.com' => 'fa-pinterest', 'qzone.qq.com' => 'fa-qq', 'reddit.com' => 'fa-reddit', 'renren.com' => 'fa-renren', 'slideshare.net' => 'fa-slideshare', 'soundcloud.com' => 'fa-soundcloud', 'spotify.com' => 'fa-spotify', 'stackexchange.com' => 'fa-stack-exchange', 'stackoverflow.com' => 'fa-stack-overflow', 'steamcommunity.com' => 'fa-steam', 'stumbleupon.com' => 'fa-stumbleupon', 't.qq.com' => 'fa-tencent-weibo', 'trello.com' => 'fa-trello', 'tumblr.com' => 'fa-tumblr', 'twitch.tv' => 'fa-twitch', 'twitter.com' => 'fa-twitter', 'vimeo.com' => 'fa-vimeo-square', 'vine.co' => 'fa-vine', 'vk.com' => 'fa-vk', 'weibo.com' => 'fa-weibo', 'weixin.qq.com' => 'fa-weixin', 'wordpress.com' => 'fa-wordpress', 'xing.com' => 'fa-xing', 'yahoo.com' => 'fa-yahoo', 'yelp.com' => 'fa-yelp', 'youtube.com' => 'fa-youtube')); // Process each menu item foreach ($sorted_menu_items as $item) { // Item classes $classes = isset($item->classes) && !empty($item->classes) ? implode(' ', (array) $item->classes) : ''; // Item target $target = isset($item->target) && $item->target ? ' target="_blank"' : ''; $item_output = ''; // Look for matching icons foreach ($supported_icons as $pattern => $class) { if (false !== strpos($item->url, $pattern)) { $item_output .= '<li class="' . esc_attr(str_replace('fa-', '', $class)) . ' ' . esc_attr($classes) . '">'; $item_output .= '<a href="' . esc_url($item->url) . '"' . $target . '>'; $item_output .= '<i class="fa fa-fw ' . esc_attr($class) . '">'; $item_output .= '<span>' . esc_html($item->title) . '</span>'; $item_output .= '</i></a></li>'; break; } } // No matching icons if ('' === $item_output) { $item_output .= '<li class="external-link-square ' . esc_attr($classes) . '">'; $item_output .= '<a href="' . esc_url($item->url) . '"' . $target . '>'; $item_output .= '<i class="fa fa-fw fa-external-link-square">'; $item_output .= '<span>' . esc_html($item->title) . '</span>'; $item_output .= '</i></a></li>'; } // Add item to list $output .= $item_output; unset($item_output); } // Email & RSS $customizer_links = ttfmake_get_social_links(); if (isset($customizer_links['email'])) { $output .= '<li class="email menu-item">'; $output .= '<a href="' . esc_url($customizer_links['email']['url']) . '">'; $output .= '<i class="fa fa-fw fa-envelope">'; $output .= '<span>' . esc_html($customizer_links['email']['title']) . '</span>'; $output .= '</i></a></li>'; } if (isset($customizer_links['rss'])) { $output .= '<li class="rss menu-item">'; $output .= '<a href="' . esc_url($customizer_links['rss']['url']) . '">'; $output .= '<i class="fa fa-fw fa-rss">'; $output .= '<span>' . esc_html($customizer_links['rss']['title']) . '</span>'; $output .= '</i></a></li>'; } // If there are menu items, add a wrapper if ('' !== $output) { $output = '<ul class="' . esc_attr($args->menu_class) . '">' . $output . '</ul>'; } return $output; }
function seattlemennonite_section() { $locations = get_nav_menu_locations(); if (isset($locations['primary'])) { $menu = wp_get_nav_menu_object($locations['primary']); // Get an array of menu items. $menu_items = wp_get_nav_menu_items($menu->term_id); _wp_menu_item_classes_by_context($menu_items); // These classes indicate that this is the current menu item. $current_classes = array('current-menu-ancestor', 'current-menu-item', 'current-post-ancestor'); // Create an array that maps the id of the menu item to // the item object. $map = array(); foreach ((array) $menu_items as $key => $menu_item) { $map[$menu_item->db_id] = $menu_item; } unset($menu_items); // Loop through the menu items looking for the current one. foreach ($map as $key => $menu_item) { if (array_intersect($current_classes, $menu_item->classes)) { return seattlemennonite_get_section($map, $menu_item); } } } }