Here’s a quick and dirty function for generating breadcrumbs based on pages in a WordPress menu. Please note that I have not tested this with menu items based on post types other than pages.
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 70 71 72 73 74 | function jki_get_nav_menu_id_for_post( $post_id ) { $items = wp_get_nav_menu_items( 'Huvudmeny' ); $menu_item_id = -1; // Find menu item foreach (( array ) $items as $menu_item ) { if ( $menu_item ->object_id == $post_id ) { $menu_item_id = $menu_item ->ID; break ; } } return $menu_item_id ; } function jki_get_nav_menu_item( $item_id ) { $args = array ( 'post_type' => 'nav_menu_item' , 'post_status' => 'publish' , 'output' => ARRAY_A, 'p' => $item_id ); $query = new WP_Query( $args ); $items = $query ->get_posts(); $items = array_map ( 'wp_setup_nav_menu_item' , $items ); if ( count ( $items ) > 0) { return $items [0]; } else { return null; } } function jki_wp_menu_breadcrumbs() { global $post ; $menu_item_id = jki_get_nav_menu_id_for_post( $post ->ID); if ( $menu_item_id == -1) return ; $current_menu_item = jki_get_nav_menu_item( $menu_item_id ); if ( $current_menu_item == null) return ; $delimiter = '<span> // </span>' ; $crumbs = array (); $crumbs [] = $delimiter . $current_menu_item ->title; if ( $current_menu_item ->menu_item_parent != 0) { $is_top = false; while ( $is_top == false) { $current_menu_item = jki_get_nav_menu_item( $current_menu_item ->menu_item_parent); $crumbs [] = $delimiter . '<a href="' . $current_menu_item ->url . '">' . $current_menu_item ->title . '</a>' ; if ( $current_menu_item ->menu_item_parent == 0) { $is_top = true; } } } $crumbs = array_reverse ( $crumbs ); echo "<div id=\"breadcrumbs\">\n" ; echo '<a href="/">Home</a>' ; foreach ( $crumbs as $crumb ) { echo $crumb ; } echo "\n</div>\n" ; } |