-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu-breadcrumbs.php
More file actions
69 lines (60 loc) · 2.02 KB
/
Copy pathmenu-breadcrumbs.php
File metadata and controls
69 lines (60 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/*
Plugin Name: Menu Breadcrumbs
Description: Creates the breadcrumbs path from the menu structure, and not from the page hierarchy. Warnung: If a page is included into a menu twice, only one (arbitrary) breadcrumb path is created
*/
function menu_breadcrumbs($menuLocation, $opt = array()) {
$opt = array_merge(array(
'echo' => false,
), $opt);
$str = '';
$menuLocationToId = get_nav_menu_locations();
if (isset($menuLocationToId[$menuLocation])) {
$menuId = $menuLocationToId[$menuLocation];
}
else {
$menuId = false;
}
if ($menuId) {
if (is_home()) {
$pageId = get_option('page_for_posts');
}
else {
$pageId = get_the_ID();
}
$currentItemId = false;
$items = wp_get_nav_menu_items($menuId);
$itemArray = array();
foreach($items as $item) {
if ($item->object_id == $pageId) {
$currentItemId = $item->ID;
}
$itemArray[$item->ID] = array('title' => $item->title, 'url' => $item->url, 'parent' => $item->menu_item_parent);
}
if ($currentItemId) {
$breadcrumbItems = array();
$breadcrumbItems[] = $itemArray[$currentItemId];
while ($breadcrumbItems[0]['parent'] > 0) {
$parentId = $breadcrumbItems[0]['parent'];
array_unshift($breadcrumbItems, $itemArray[$parentId]);
}
$countBCItems = count($breadcrumbItems);
$str .= '<nav class="breadcrumbs">';
foreach ($breadcrumbItems as $i => $bci) {
if ($i < ($countBCItems - 1)) {
$str .= '<span><a href="' . $bci['url'] . '">' . $bci['title'] . '</a></span>';
}
else {
$str .= '<span class="last">' . $bci['title'] . '</span>';
}
}
$str .= '</nav>';
}
}
if ($opt['echo']) {
echo $str;
}
else {
return $str;
}
}