View difference between Paste ID: YbY1wxSa and C6gNEi21
SHOW: | | - or go back to the newest paste.
1
/**
2
 * Custom Main Menu Walker
3
 */
4
class Main_Nav_Walker extends Walker_Nav_Menu {
5
	function check_current($classes) {
6
		return preg_match('/(current[-_])|active|dropdown/', $classes);
7
	}
8
	function start_lvl(&$output, $depth = 0, $args = array()) {
9
		$output .= "\n<ul class=\"dropdown-menu\">\n";
10
	}
11
	function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
12
		parent::start_el($item_html, $item, $depth, $args);
13
		if ($item->is_dropdown && ($depth === 0)) {
14
			$item_html = str_replace('<a', '<a class="dropdown-toggle" data-toggle="dropdown" data-target="#"', $item_html);
15
			$item_html = str_replace('</a>', ' <b class="caret"></b></a>', $item_html);
16
		} elseif (stristr($item_html, 'li class="divider')) {
17
			$item_html = preg_replace('/<a[^>]*>.*?<\/a>/iU', '', $item_html);
18
		} elseif (stristr($item_html, 'li class="dropdown-header')) {
19
			$item_html = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $item_html);
20
		}
21
		$item_html = apply_filters('roots_wp_nav_menu_item', $item_html);
22
		$output .= $item_html;
23
	}
24
	function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
25
		// Add anchors as children elements
26
		if ($depth == 0){
27
			$elementid 	= $element->ID;
28
			$objectid 	= $element->object_id;
29
			// Get subitems
30
			$subitems = get_field('sub_items', $objectid);
31
			if ($subitems) {
32
				// Construct child array from anchors
33
				$children_elements = '';
34
				foreach ($subitems as $wp_post) {
35
					$item_title 	= $wp_post['sub_item_title'];
36
					$item_anchor 	= sanitize_title($item_title);
37
					// Create new Array for each item
38
					$children_elements[$elementid][] = array(
39
						'ID' 					=> 124,
40
						'post_title' 				=> $item_title,
41
						'post_name' 				=> $item_anchor,
42
						'post_type' 				=> 'nav_menu_item',
43
						'db_id' 				=> 124,
44
						'menu_item_parent' 			=> $elementid,
45
						'object_id' 				=> 124,
46
						'object' 				=> 'custom',
47
						'type' 					=> 'custom',
48
						'type_label' 				=> 'Anchor',
49
						'title' 				=> $item_title,
50
						'url' 					=> '#'.$item_anchor,
51
						'target' 				=> '',
52
						'attr_title' 				=> $item_title
53
					);				  
54
				}
55
				//print_r($children_elements);
56
			}	
57
		}
58
59
		$element->is_dropdown = ((!empty($children_elements[$element->ID]) && (($depth + 1) < $max_depth || ($max_depth === 0))));
60
		if ($element->is_dropdown) {
61
			$element->classes[] = 'dropdown';
62
		}
63
		parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
64
	}
65-
}
65+
}
66
67
// Call custom menu
68
if (has_nav_menu('primary_navigation')) :
69
	wp_nav_menu(array('theme_location' => 'primary_navigation', 'menu_class' => 'main-menu nav navbar-nav col-md-8', 'walker' => new Main_Nav_Walker() ));
70
endif;