{"id":113,"date":"2013-01-21T18:44:53","date_gmt":"2013-01-21T18:44:53","guid":{"rendered":"http:\/\/tier7.net\/blog\/?p=113"},"modified":"2013-01-21T18:44:53","modified_gmt":"2013-01-21T18:44:53","slug":"jquery-bread-crumb-trail-plugin","status":"publish","type":"post","link":"https:\/\/tier7.net\/blog\/2013\/01\/jquery-bread-crumb-trail-plugin\/","title":{"rendered":"jQuery Bread Crumb Trail Plugin"},"content":{"rendered":"<p>This plugin will take the URI segment of the URL and split it into links based on directory names. This is not the most attractive method of doing this, but is very useful when trying to add breadcrumbs to older websites that have a good document hierarchy.<\/p>\n<pre>\/*\r\n\u00a0 Generate breadcrumbs based on URI\r\n*\/\r\n(function( $ ){\r\n\u00a0 $.fn.breadCrumb = function( options ) {\r\n\u00a0\u00a0 \u00a0\u00a0 \/\/---------------------------------------------------------------\r\n\u00a0\u00a0 \u00a0\u00a0 \/\/ jQuery breadCrumb v0.1\r\n\u00a0\u00a0 \u00a0\u00a0 \/\/---------------------------------------------------------------\r\n\u00a0\u00a0 \u00a0\u00a0 var $obj = this;\r\n\u00a0\u00a0 \u00a0\u00a0 var settings = {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 'home'\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0: \"Home\",\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 'seperator'\u00a0\u00a0 \u00a0: \"&amp;gt;\",\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 'title'\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0: document.title\r\n\u00a0\u00a0 \u00a0\u00a0 };\r\n\u00a0\u00a0 \u00a0\u00a0 var settings = $.extend(settings, options);\r\n\u00a0\u00a0 \u00a0\u00a0 var $trail = \"\";\r\n\u00a0\u00a0 \u00a0\u00a0 var $path = document.location.pathname.replace(\/%20\/gi, \" \");\r\n\u00a0\u00a0 \u00a0\u00a0 var $originalContents = $obj.html();\r\n\u00a0\u00a0 \u00a0\u00a0 var $title = settings.title;\r\n\u00a0\u00a0 \u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\/\/each implementation\r\n\u00a0\u00a0 \u00a0this.each(function(){\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$directories = $path.split(\"\/\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$directories[0] = settings.home;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0pathRebuild = \"\/\";\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/first breadcrumb is link to home\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$trail += '&lt;a href=\"\/\"&gt;' + settings.home + '&lt;\/a&gt; ' + settings.seperator + \" \";\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/loop through subsequent directories skipping the top level document\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0for ( i = 1; i &lt; $directories.length - 1; i++ ){\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0pathRebuild += $directories[i] + '\/';\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$trail += '&lt;a href=\"' + pathRebuild + '\"&gt;' + $directories[i] + '&lt;\/a&gt; ' + settings.seperator + \" \";\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/$obj.html($trail + $originalContents);\u00a0\u00a0 \u00a0\/\/alternative\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$obj.html($trail + $title);\u00a0\u00a0 \u00a0\/\/use document title\r\n\u00a0\u00a0 \u00a0});\r\n\u00a0 }\r\n})( jQuery );<\/pre>\n<h2>Usage<\/h2>\n<p>Under your jQuery $(document).ready statement, use the following to initialize the breadcrumb trail into the DOM Object of your choosing. In this case we are using an element with id=&#8221;jquery-breadcrumb&#8221;<\/p>\n<pre>$(\"#jquery-breadcrumb\").breadCrumb({'home':'Website Name'});<\/pre>\n<h2>Live Demo<\/h2>\n<p><a title=\"http:\/\/jsfiddle.net\/BamA8\/\" href=\"http:\/\/jsfiddle.net\/BamA8\/\" target=\"_blank\">http:\/\/jsfiddle.net\/BamA8\/<\/p>\n<p><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This plugin will take the URI segment of the URL and split it into links based on directory names. This is not the most attractive method of doing this, but is very useful when trying to add breadcrumbs to older &hellip; <a href=\"https:\/\/tier7.net\/blog\/2013\/01\/jquery-bread-crumb-trail-plugin\/\">Continue reading <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":[1],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":3,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":116,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions\/116"}],"wp:attachment":[{"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}