Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Custom Main Menu Walker
- */
- class Main_Nav_Walker extends Walker_Nav_Menu {
- function check_current($classes) {
- return preg_match('/(current[-_])|active|dropdown/', $classes);
- }
- function start_lvl(&$output, $depth = 0, $args = array()) {
- $output .= "\n<ul class=\"dropdown-menu\">\n";
- }
- function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
- parent::start_el($item_html, $item, $depth, $args);
- if ($item->is_dropdown && ($depth === 0)) {
- $item_html = str_replace('<a', '<a class="dropdown-toggle" data-toggle="dropdown" data-target="#"', $item_html);
- $item_html = str_replace('</a>', ' <b class="caret"></b></a>', $item_html);
- } elseif (stristr($item_html, 'li class="divider')) {
- $item_html = preg_replace('/<a[^>]*>.*?<\/a>/iU', '', $item_html);
- } elseif (stristr($item_html, 'li class="dropdown-header')) {
- $item_html = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $item_html);
- }
- $item_html = apply_filters('roots_wp_nav_menu_item', $item_html);
- $output .= $item_html;
- }
- function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
- // Add anchors as children elements
- if ($depth == 0){
- $elementid = $element->ID;
- $objectid = $element->object_id;
- // Get subitems
- $subitems = get_field('sub_items', $objectid);
- if ($subitems) {
- // Construct child array from anchors
- $children_elements = '';
- foreach ($subitems as $wp_post) {
- $item_title = $wp_post['sub_item_title'];
- $item_anchor = sanitize_title($item_title);
- // Create new Array for each item
- $children_elements[$elementid][] = array(
- 'ID' => 124,
- 'post_title' => $item_title,
- 'post_name' => $item_anchor,
- 'post_type' => 'nav_menu_item',
- 'db_id' => 124,
- 'menu_item_parent' => $elementid,
- 'object_id' => 124,
- 'object' => 'custom',
- 'type' => 'custom',
- 'type_label' => 'Anchor',
- 'title' => $item_title,
- 'url' => '#'.$item_anchor,
- 'target' => '',
- 'attr_title' => $item_title
- );
- }
- //print_r($children_elements);
- }
- }
- $element->is_dropdown = ((!empty($children_elements[$element->ID]) && (($depth + 1) < $max_depth || ($max_depth === 0))));
- if ($element->is_dropdown) {
- $element->classes[] = 'dropdown';
- }
- parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
- }
- }
- // Call custom menu
- if (has_nav_menu('primary_navigation')) :
- wp_nav_menu(array('theme_location' => 'primary_navigation', 'menu_class' => 'main-menu nav navbar-nav col-md-8', 'walker' => new Main_Nav_Walker() ));
- endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement