wordpress breadcrumbs according to menu structure
// helper function to find a menu item in an array of items
function wpd_get_menu_item( $field, $object_id, $items ){
foreach( $items as $item ){
if( $item->$field == $object_id ) return $item;
}
return false;
}
function wpd_nav_menu_breadcrumbs( $menu ){
// get menu items by menu id, slug, name, or object
$items = wp_get_nav_menu_items( $menu );
if( false === $items ){
echo 'Menu not found';
return;
}
// get the menu item for the current page
$item = wpd_get_menu_item( 'object_id', get_queried_object_id(), $items );
if( false === $item ){
return;
}
// start an array of objects for the crumbs
$menu_item_objects = array( $item );
// loop over menu items to get the menu item parents
while( 0 != $item->menu_item_parent ){
$item = wpd_get_menu_item( 'ID', $item->menu_item_parent, $items );
array_unshift( $menu_item_objects, $item );
}
// output crumbs
$crumbs = array();
foreach( $menu_item_objects as $menu_item ){
$link = '<a href="%s">%s</a>';
$crumbs[] = sprintf( $link, $menu_item->url, $menu_item->title );
}
echo join( ' > ', $crumbs );
}
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line