{"id":93,"date":"2012-06-21T14:54:11","date_gmt":"2012-06-21T13:54:11","guid":{"rendered":"http:\/\/jonas.kihlsten.se\/blog\/?p=93"},"modified":"2012-08-30T14:15:38","modified_gmt":"2012-08-30T13:15:38","slug":"breadcrumbs-based-on-wordpress-menus","status":"publish","type":"post","link":"http:\/\/jonas.kihlsten.se\/blog\/2012\/06\/breadcrumbs-based-on-wordpress-menus\/","title":{"rendered":"Breadcrumbs based on WordPress menus"},"content":{"rendered":"<p>Here&#8217;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.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction jki_get_nav_menu_id_for_post($post_id)\r\n{\r\n\t$items = wp_get_nav_menu_items('Huvudmeny');\r\n\t\r\n\t$menu_item_id = -1;\r\n\t\r\n\t\/\/ Find menu item\r\n\tforeach ((array)$items as $menu_item) {\r\n\t\tif($menu_item-&gt;object_id == $post_id) {\r\n\t\t\t$menu_item_id = $menu_item-&gt;ID;\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n\t\r\n\treturn $menu_item_id;\r\n}\r\n\r\nfunction jki_get_nav_menu_item($item_id)\r\n{\r\n\t$args = array( 'post_type' =&gt; 'nav_menu_item', \r\n\t    'post_status' =&gt; 'publish', \r\n\t    'output' =&gt; ARRAY_A, \r\n\t    'p' =&gt; $item_id);\r\n\t\r\n\t$query = new WP_Query($args);\r\n\t$items = $query-&gt;get_posts();\r\n\t$items = array_map( 'wp_setup_nav_menu_item', $items );\r\n\t\r\n\tif(count($items) &gt; 0) {\r\n\t\treturn $items&#x5B;0];\r\n\t}\t\r\n\telse {\r\n\t\treturn null;\r\n\t}\r\n}\r\n\r\nfunction jki_wp_menu_breadcrumbs()\r\n{\t\r\n\tglobal $post;\r\n\r\n\t$menu_item_id = jki_get_nav_menu_id_for_post($post-&gt;ID);\r\n\t\r\n\tif($menu_item_id == -1) return;\r\n\t\r\n\t$current_menu_item = jki_get_nav_menu_item($menu_item_id);\r\n\t\r\n\tif($current_menu_item == null) return;\r\n\t\r\n\t$delimiter = '&lt;span&gt; \/\/ &lt;\/span&gt;';\r\n\t$crumbs = array();\r\n\t\r\n\t$crumbs&#x5B;] = $delimiter . $current_menu_item-&gt;title;\r\n\t\r\n\tif($current_menu_item-&gt;menu_item_parent != 0) {\r\n\t\t$is_top = false;\r\n\t\twhile($is_top == false) {\r\n\t\t\t$current_menu_item = jki_get_nav_menu_item($current_menu_item-&gt;menu_item_parent);\r\n\t\t\t$crumbs&#x5B;] = $delimiter . '&lt;a href=&quot;' . $current_menu_item-&gt;url . '&quot;&gt;' . $current_menu_item-&gt;title . '&lt;\/a&gt;';\r\n\t\t\t\r\n\t\t\tif($current_menu_item-&gt;menu_item_parent == 0) {\r\n\t\t\t\t$is_top = true;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t\r\n\t$crumbs = array_reverse($crumbs);\r\n\t\r\n\techo &quot;&lt;div id=\\&quot;breadcrumbs\\&quot;&gt;\\n&quot;;\r\n\techo '&lt;a href=&quot;\/&quot;&gt;Home&lt;\/a&gt;';\r\n\tforeach ($crumbs as $crumb) {\r\n\t\techo $crumb;\r\n\t}\r\n\techo &quot;\\n&lt;\/div&gt;\\n&quot;;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;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. function jki_get_nav_menu_id_for_post($post_id) { $items = wp_get_nav_menu_items(&#8216;Huvudmeny&#8217;); $menu_item_id = -1; \/\/ Find menu item foreach ((array)$items as $menu_item) { if($menu_item-&gt;object_id == $post_id) &hellip; <a href=\"http:\/\/jonas.kihlsten.se\/blog\/2012\/06\/breadcrumbs-based-on-wordpress-menus\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Breadcrumbs based on WordPress menus<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[30,17],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-wordpress","tag-wp_nav_menu"],"_links":{"self":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":11,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":6108,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions\/6108"}],"wp:attachment":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}