NOTE: This functionality is now available in Drupal 7 via the Breadcrumbs by Path or the Path Breadcrumbs modules. See differences here: #2012762: How does breadcrumbs_by_path compare with path_breadcrumbs module?
The following code will allow you to create a breadcrumb trail that matches the URL of the current page. For example, if the current URL is
http://www.example.com/photos/flowers/my_rose_garden
then the breadcrumb trail would be
Home > Photos > Flowers > My Rose Garden
This technique is best used on smaller sites as each part of the URL must point to a valid page, view, etc.
Add the following code to your theme's template.php file (make sure you replace [YOUR-THEME-NAME] with the name of your theme):
<?php
/**
* Override theme_breadcrumb().
*/
function [YOUR-THEME-NAME]_breadcrumb($breadcrumb) {
$links = array();
$path = '';
// Get URL arguments
$arguments = explode('/', request_uri());
// Remove empty values
foreach ($arguments as $key => $value) {
if (empty($value)) {
unset($arguments[$key]);
}
}
$arguments = array_values($arguments);
// Add 'Home' link
$links[] = l(t('Home'), '<front>');
// Add other links
if (!empty($arguments)) {
foreach ($arguments as $key => $value) {
// Don't make last breadcrumb a link
if ($key == (count($arguments) - 1)) {
$links[] = drupal_get_title();
} else {
if (!empty($path)) {
$path .= '/'. $value;
} else {
$path .= $value;
}
$links[] = l(drupal_ucfirst($value), $path);
}
}
}
// Set custom breadcrumbs
drupal_set_breadcrumb($links);
// Get custom breadcrumbs
$breadcrumb = drupal_get_breadcrumb();
// Hide breadcrumbs if only 'Home' exists
if (count($breadcrumb) > 1) {
return '<div class="breadcrumb">'. implode(' » ', $breadcrumb) .'</div>';
}
}
?>
Comments
Will this work for 5x?
Will this work for 5x?
It should...
It should, though I haven't tested it. A quick check of the functions shows they're all the same for Drupal 5 & 6.
Thanks! Does the job for me
Thanks! Does the job for me
?q= in primary links
If i use this good script, when i click on a my primary links appear "?q="..
Someone know why?
Thanks
A small correction
This is a great script. However, the example will come out as:
Home > Photos > Flowers > My_rose_garden
To fix this one can add:
$value = str_replace("_", " ", $value);
just before
$links[] = l(drupal_ucfirst($value), $path);
better use $arguments =
better use
instead of
$arguments = explode('/', request_uri());
for use if your urls are with ?q=....
and i recommend to use the pathauto module to have links (and breadcrumbs using this snippet then) for example like "content/node-title" not "node/234"
Nice job!
Verry nice my friend, thanks for sharing!
http://www.wbase.be twitter: @wbase_be
Adjustment for i18n & valid url
I made a small adjustment to this code.
1. When the site is multilangual and you put the language in your path remove it from the breadcrumb
2. Check or the url is valid, when it is not valid (for example: special menu items) don't make a link from it.
http://www.wbase.be twitter: @wbase_be
Adjustment for showing the node title
I made another adjustment on the code so that the node title is printed instead of the value from the url
http://www.wbase.be twitter: @wbase_be
d7
Hello, it is not working in d7 with ZEN subtheme. Can you help me?
I modified the code to suit
I modified the code to suit my website (with image gallery). I'm sharing the code in case someone could use it.
Be sure to change the languages (if ($value == '[Place_language_code]') to the languages you use.
This code: Hides the language code, places the right links, makes all links clickable.
thank you!
I have been looking for a breadcrumb snippet like this for so long and this is exactly what I need!
Perfect!
Flawless in 7.4 Thanks!
Remove current path name from breadcrumb?
Would be nice to be able to remove the current taxonomy/node name from the breadcrumb.
So if I at home >> products only home is showing.
Same if I'm at home >> products >> product foo don't show product foo in the breadcrumb as there's already a title for the page anyways.
A Big Thank You!!!
I just want to say that I tested this with 7.12 on a sub-theme based on Bartik, and it still works beautifully! Just what I've been looking for in a long time! Thanks for providing the code and clear instructions!
Breadcrumbs by Path
I've just updated the page above with a link to the Breadcrumbs by Path module. It provides this functionality for Drupal 7 with no configuration necessary, just enable the module!
Version using menu item titles
And here's my version, which looks for the title of the page as it would show in the menu. I guess one could also add a fallback to look up the node title, as others have done. Note that menu_get_item() does load the entire node (if that's what you're viewing) within the page_arguments array (in $menu_item in this example). This all seems kind of inefficient... uggh.
I just used this on a D6 site
I just used this on a D6 site with a few minor changes…
I changed...
$links[] = l(drupal_ucfirst($value), $path);
to
$value = str_replace("-", " ", $value);
$links[] = l(ucwords($value), $path);
And added a few special cases via...
if (($arguments[0] == ‘path-one') || $arguments[0] == ‘path-two') {
$arguments[0] = ‘betterpath';
}
And it all worked great.
Thanks!