background image
Eric's picture

Here's a quick code snippet that shows you how you can create custom breadcrumbs for a certain node type. This code would reside in your template.php theme file.

<?php
function MYTHEME_preprocess_page(&$variables) {
  if (
$variables['node']->type == 'MYNODETYPE') {
       
   
$links = array();

   
// creating a link to the home page
   
$links[] = l('Home', '<front>');

   
// here's how you could add a link to a taxonomy page
   
$vid = 2;
    foreach (
$variables['node']->taxonomy as $k => $v) {
      if (
$v->vid = $vid) {  
       
$links[] = l($v->name, 'taxonomy/term/' . $v->tid);
        break;
      }
    }
       
   
// yet another link
   
$links[] = l('Some Other Link', 'SOMEOTHERLINK');

   
// lastly, overwrite the contents of the breadcrumbs variable in the page scope
   
$variables['breadcrumb'] = theme('breadcrumb', $links);
  }
}
?>

NOTE: if you were adding this code to a module, you could use the drupal_set_breadcrumb() function to do the same functionality.

Thank you

Thank you

worked good

Thanks!

An easy step to make things work

Thank you Eric for this ( to me ) easy way getting things work the way they should be. I'm working on a site where I don't use taxonomy and have one content type for more categories, to this code of yours isn't exactly what I need, but.. it's easy to adapt.

Instead of node type, I use node id to get what I want, this is fine for smaller sites, since the code in the template.php would be too much. Here's an example of what I have

<?php
function phptemplate_preprocess_page(&$variables)
{
   
// subpage of the first category for example
   
if ($variables['node']->nid == '2')
    {
       
$links = array();
       
$links[] = l('Category 1', 'caregory-one');
       
$variables['breadcrumb'] = theme('breadcrumb', $links);
    }
   
// subpages for ths second category
   
if ($variables['node']->nid == '4' || '5' || '6')
    {
       
$links = array();
       
$links[] = l('Home', '<front>');
       
$links[] = l('Category 1', 'category-two');
       
$variables['breadcrumb'] = theme('breadcrumb', $links);
    }

}
?>

I'm not sure if this is the right approach but for smaller websites where no new content is added is this just fine.

Thanks for the above code and if there's something I could do in a better way... I'm open for more