Giter Site home page Giter Site logo

Comments (4)

CullenJWebb avatar CullenJWebb commented on May 27, 2024 1

Thank you @IanDelMar ! That has put me on the right track.

from wp-bootstrap-navwalker.

IanDelMar avatar IanDelMar commented on May 27, 2024

I'm having trouble creating a function that takes a parent menu item and adds an exact copy as the first item of its own drop-down menu.

As a clickable menu item or just like a header?

an exact copy

means a dropdown if it is a dropdown? But then you'd be creating a menu of infinite depth.

from wp-bootstrap-navwalker.

CullenJWebb avatar CullenJWebb commented on May 27, 2024

As a clickable menu item or just like a header?

Clickable menu item.

an exact copy

means a dropdown if it is a dropdown? But then you'd be creating a menu of infinite depth.

Exact as far as the text, data-bs-toggle, classes, etc., not the dropdown within the parent.

from wp-bootstrap-navwalker.

IanDelMar avatar IanDelMar commented on May 27, 2024

@CullenJWebb please note that this question is totally unrelated to the WP Bootstrap Navwalker. However, try this or use it as start for further adjustments. The code loops through all menu items of a menu associated with the theme location primary. If and only if a menu item has the class menu-item-has-children it is considered to be a parent menu item. It is then cloned and added to the array of menu items as it's own child.

add_action( 'wp_nav_menu_objects', 'slug_clone_parent_menu_item', 10, 2 );
function slug_clone_parent_menu_item( $sorted_menu_items, $args ) {
	// Only apply modifications for the theme location 'primary'.
	if ( 'primary' !== $args->theme_location ) {
		return $sorted_menu_items;
	}

	$new_items  = array();
	$menu_order = 1;
	foreach ( $sorted_menu_items as $item ) {
		$item->menu_order = $menu_order;
		$new_items[]      = $item;

		if ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
			$parent_clone = clone $item;
			foreach ( $parent_clone->classes as $key => $value ) {
				if ( 'menu-item-has-children' === $value ) {
					unset( $parent_clone->classes[ $key ] );
				}
			}

			$parent_clone->menu_item_parent = (string) $item->ID;
			$parent_clone->menu_order       = ++$menu_order;

			$new_items[] = $parent_clone;
		}
		++$menu_order;
	}
	return $new_items;
}

Please consider closing the issue if the proposed solution solves your problem.

from wp-bootstrap-navwalker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.