Generating Divi Breadcrumbs from a Menu

|

The Divi Breadcrumbs Module lets you add SEO-ready breadcrumbs to any Divi builder layout. By default, the breadcrumbs are based on the structure of the site as it is defined in WordPress (e.g. using categories, tags, parent pages, and so on). But what if you need the breadcrumbs to match the the structure of a custom menu, such as the Divi header menu or Divi menu module? For example, this one:

While there isn't currently a way to do it within the plugin, it's possible to achieve it with the following PHP code:

// === Generate breadcrumbs based on menu structure ===

add_filter('et_pb_dmb_breadcrumbs_crumbs', 'dbc_generate_crumbs_from_menu');

function dbc_generate_crumbs_from_menu($crumbs) {
	
	$menu_name = 'Divi Menu';
	
	$menu = DBC_MenuItems::from_menu($menu_name);
	
	$obj_id = get_queried_object_id();
	if (empty($obj_id)) { return $crumbs; }
	
	$current_url = get_permalink($obj_id);
	if (empty($current_url)) { return $crumbs; }
	
	$item = $menu->find_by_url($current_url);
	if ($item) {
		$parents = $menu->get_parents($item);
		$new_crumbs = array($crumbs[0]); // Home
		foreach($parents as $k=>$v) {
			$new_crumbs[] = array(
				'text' => $v->title,
				'url' => $v->url
			);
		}
		$new_crumbs[] = $crumbs[count($crumbs)-1]; // last (page) crumb
		$crumbs = $new_crumbs;
	}
	return $crumbs;
}

class DBC_MenuItems {
	
	private $items;
	
	static function from_menu($name) {
		$items = wp_get_nav_menu_items($name);
		return new self($items?$items:array());
	}
	
	private function __construct($items) {
		$this->items = $items;
	}
	
	public function get_parents($item) {
		$parents = array();
		while ($item = $this->get_parent($item)) {
			array_unshift($parents, $item);
		}
		return $parents;
	}
	
	public function get_parent($item) {
		return empty($item->menu_item_parent)?false:$this->find_by_id($item->menu_item_parent);
	}
	
	public function find_by_id($id) {
		foreach($this->items as $k=>$item) {
			if ($item->ID == $id) {
				return $item;
			}
		}
		return false;
	}
	
	public function find_by_url($url) {
		foreach($this->items as $k=>$item) {
			if ($item->url === $url) {
				return $item;
			}
		}
		return false;
	}
}

// === END ===

The only modification you should need to make to the code is to set the name of your menu, which you can do by modifying the line that reads:

$menu_name = 'Divi Menu';

replacing 'Divi Menu' with the name of your menu, as defined in WP Dashboard > Appearance > Menus.

The code works by retrieving the structure of the menu, and looking for any menu item whose URL matches the current page's URL. If it finds one, it then retrieves the item's parent items from the menu and converts them into breadcrumbs. Finally these are inserted between the home breadcrumb and the current page breadcrumb using a filter provided by Divi Breadcrumbs Module, to produce the result, e.g.

Note that the final breadcrumb is taken from the page title, rather than the page's entry in the menu. 

This post may contain referral links which may earn a commission for this site

Divi Booster

Hundreds of new features for Divi
in one easy-to-use plugin

0 Comments

Submit a Comment

Your email address will not be published.