Sep 12 2006

Drupal Easy Breadcrumb Template


Categories:

Simple Directory-Based Drupal Breadcrumb Template

While Drupal breadcrumb functionality and support has greatly advanced, it still requires in-depth knowledge of the taxonomy system and several modules must be installed to get them working properly. Some of the problems I encountered were path names not reflecting page titles, paths appearing where I did not want them to, and paths linking to the front page (I do not want pages linking to themselves in the breadcrumb path for SEO reasons). Most of these issues can be resolved using the taxonomy_breadcrumb module, but additional work is needed to layout and style the breadcrumb path. If you're looking for a simple, directory-based breadcrumb path, you may find this useful. This script involves simply adding a template file to your theme. Here are two variations - one for sites that don't utilize a front page with promoted nodes, and one for sites that do.

Breadcrumb Template for Static Front Page Site

<?php
function breadcpath($page_title,$site_name) {   
 
$dir_url = $_REQUEST['q'];
 
$pathdata = 'example.com/'.$dir_url; // substitute your domain name here
   
$pathArray = explode("/",$pathdata);
   
$pathdataspace = str_replace("-", " ", $pathdata);
   
$pathSpaceArray = explode("/",$pathdataspace);
   
$newBCPath = $root_url."/";
  if(
$dir_url != "") {
    
$breadCrumb = '<a href="/" title="'.$site_name.'">'.$site_name.'</a> &raquo; ';
      for(
$a=1;$a<count($pathArray)-1;$a++) {
         
$BCName = ucwords($pathSpaceArray[$a]);
         
$newBCPath .= $pathArray[$a].'/';
         
$breadCrumb .= '<a href="'.$newBCPath.'">'.$BCName.'</a> &raquo; ';
      }
  }
   
$breadCrumb .= '<strong>'.$page_title.'</strong>';
    print(
$breadCrumb);
    return
true;
}
?>

Breadcrumb Template for Site With Promoted Nodes

Same as above, but move the $breadCrumb declaration inside the if statement:

<?php
...
         
$breadCrumb .= '<a href="'.$newBCPath.'">'.$BCName.'</a> &raquo; ';
      }
   
$breadCrumb .= '<strong>'.$page_title.'</strong>';
  }
    print(
$breadCrumb);
        return
true;
}
?>

How to Use the Breadcrumb Template

Simply copy and paste the above code into a file "template.php," place it in your theme directory and substitute "example.com" on the third line with your domain name. Then you will need to call the function breadcpath($title, $site_name) from your file of choice (I call it from node.tpl.php because I want the breadcrumb to display inside the content node) and pass in $page_title and $site_name variables, as in the example. Post any questions/ comments below.

Average: 4 (3 votes)
Select your preferred way to display the comments and click "Save settings" to activate your changes.

Helpful content

A must-read for Drupal developers, this functionality should be in core!

Thanks

For some reason

It displays a number 1 at the end of my breadcrumb. Example:

My Site » Member » admin1

What could be causing this?

My guess is that you typed

My guess is that you typed "print true" by accident (which would return '1'), instead of "return true" (on the last line).

If that's not the case make sure you didn't accidentally include a 1 somewhere in the script.

I've stopped using this script in favor of Drupal's built-in breadcrumb module. As you can see all my links have been rewritten as "permalinks" and they have been categorized in various categories. I find this increases site portability and doesn't make my links dependent on fixed directory structures.

Smiling

Thanks from me too

The spurious '1' occurs if you call the function from a print statement (print breadcpath(...)).

I tried the built in breadcrumb module but it only works if you link your pages to the navigation menu in the same hierarchy as your url paths, which means you have to enter the same hierarchy info twice.

$pathdataspace = preg_replace('/[-_]/', ' ', $pathdata);

caters for underscores in the path as well as hyphens.

problem

Doesn't work very well if your site is in a subfolder. e.g I see:

» ~mh » Drupal » Products » Windows Mobile Pocket PC

Because my path is http://localhost/~mh/drupal/?q=products/pocketpc

Ideally I'd like to see:

Home » Products » Windows Mobile Pocket PC

This is awesome!

THANKS!

100% agree with kc.

this functionality should be in core!

How to call the function?

"(...)Then you will need to call the function breadcpath($title, $site_name) from your file of choice (...) and pass in $page_title and $site_name variables (...)"

Hello,

I want to know how to call the function, in my case, i want to call it from page.tpl.php.

Regards

Calling the breadcrumb function

Hi there,

Call the function like so in your page.tpl.php file:

<?php breadcpath($title, $site_name) ?>

Insert that snippet wherever you want the breadcrumb path to appear.

Smiling

this doesnt work for me unfortunatelly

hi,

the only path i get by using this script is something like "node >>node >> 1"
there is really just word node, nothing else.

i dont know much about this, so probably i have to just stick to standard drupal "functionality" or can u help? thank you very much Smiling

Drupal version?

Hi there,

What Drupal version are you using? This script has not been tested with Drupal 5.x. My team and I are leaving on a business trip for the next 1.5 weeks so we'll check back when we're back in town in the event that no one else answers in the interim.

Cheers,
Alex

drupal version

Hi,
thanks for your answer. I am using Drupal 5.1, so maybe thats the reason...

Paul

Drupal 5.1

Thanks,
Works fine in 5.1, only tested the promoted node version tho.

still too complicated

I am not generally known as a dolt, but I'm still finding everything I've read about breadcrumbs to be too complicated.

I operate a very hierarchical site. It is expected that it would have breadcrumbs. My SEO guys are insisting upon it. The crumbs I need are partially there, by default, in Drupal. The things I need to change are:

1) include current page in breadcrumb trail (I don't care whether active link or not)
2) change "home" to the domain name

That's it! I want nothing else. If anyone has an extremely simple way to effect this, I would be very happy.

I keep getting syntax errors

Hi,

I copied and pasted the above script into my template file trying it with and with out the <?php ?> tags and I kept getting a syntax error. I got the white screen of death so to speak. I'm modifying zen's starter kit template. Do you have any idea why I might be getting this error?

Thanks for any help!
Becky

Breadcrumb script for Drupal

Hi Becky,

When you say you're modifying "Zen's starter kit" are you referring to the Zen shopping cart software? This script was written for the Drupal CMS.

Cheers,
Alex

No. Zen's theme provides a

No. Zen's theme provides a starter kit which lets people design themes using a zen foundation. Sorry it took me so long to reply to this. I actually think my issue is node hierarchy. I can't make one node the parent of another and because of that, the breadcrumbs can't reflect it either. So for example, if in a menu i have a hierarchy where "member roster" is a parent to "Global Organization" , the nodes this content resides on is not hierarchical so there isn't a real parent/child relationship going on.

Simplest of breadcrumb templates

Actually all this template does is extract names out of the URL path. So, for example, if you have name/folder/file, the breadcrumb will be printed as name -> folder -> file. Therefore, the underlying hierarchy/ taxonomy doesn't play a role. I wouldn't be surprised, however, if there's been enough development in Drupal's taxonomy modules to enable them to cater to what you need.

A Drupal breadcrumb template that works!

Thanks a lot,

finally a breadcrumb template which work like it is supposed to.
I've been researching for a while to find this.

Cheers,
Bart

PS: Make sure you clean the cache after implementing the template, because it can show error at first.

drupal breadcrumb link

I do not commonly leave commentaries on posts that I read, but I desired to tell you that you have a very nice composition style.

I had to take a instant and thank you for the post you wrote.

Haha, nice spam

Every now and then people's spam attempts crack me up. I'm sure you don't "commonly" post - you simply spam. The fact that your post, which was about "home for sale" and contained a link we removed, was completely off topic, didn't key us in at all... Eye-wink

lol

I get the same thing on various Drupal sites I run. Most spam is just annoying but occasionally I get one that really makes me chuckle. I get it most in Forums strangely enough.

I see your using Captcha (same as me) so how did the spam get through? I use the Captcha pack which has a few more options for Captcha you might want to look at it if you havent already.

Cheers and keep up the good work!

Nick
www.projex.com

Image captcha

Hi Nick,

I think we're currently using image captcha. We might simply need to adjust the font to be more grainy and distorted, etc. Thx for the heads up on captcha pack - I'll check it out.

Also, sometimes someone keys in the captcha and then manually posts crap.

Cheers,
Alex

I made some changes...

Instead of it saying Node it now has the logic if Node grad last destination url. I left Pathdata configured with the real url so you can go look. Try the jump to menu on the left.

function breadcpath($page_title, $site_name) {  
  $past = $_SESSION['destionation_past'];
  $destination_past = drupal_get_path_alias($_GET['q']);
  $_SESSION['destionation_past'] = $destination_past;
  $dir_url = $_REQUEST['q'];
  $pathdata = 'subdomain.example.com/'.$dir_url; // substitute your domain name here
    $pathArray = explode("/",$pathdata);
    $pathdataspace = str_replace("-", " ", $pathdata);
    $pathSpaceArray = explode("/",$pathdataspace);
    $newBCPath = $root_url."/";
  if($dir_url != "") {
     $bread_custom = '<a href="/" title="'.$site_name.'">'.$site_name.'</a> &raquo; ';
      for($a=1;$a<count($pathArray)-1;$a++) {
          $BCName = ucwords($pathSpaceArray[$a]);
          $newBCPath .= $pathArray[$a].'/';
  if($BCName == 'Node') {
    //$destination[] = drupal_get_path_alias($_GET['q']);
    //past has / so I need to break at these to make titles and paths
    //
      $trim = TRUE;
      $pastdata = explode("/",$past);
      for($b=0;$b<count($pastdata);$b++) {
      $title = $pastdata[$b];
      $bread_custom .= '<a href="'.$base_url.'/';
      if($b >= '1') { $bread_custom .= $path_previous; }
      $bread_custom .= ''.$title.'">'.$title.'</a> &raquo; ';
      $path_previous .= "$title/";
      }
    } else {
    $bread_custom .= '<a href="'.$newBCPath.'">'.$BCName.'</a> &raquo; ';
  }
      }
  }
    //$breadCrumb .= '<strong>'.$page_title.'</strong>';
    $bread_custom = substr($bread_custom, 0, -9);
    print($bread_custom);
    //return true;
}

If it is not already, we should post this as a module.

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

Captcha
This question is used to make sure you are a human visitor and to prevent spam submissions.
Copy the characters (respecting upper/lower case) from the image.