Last updated March 5, 2010. Created on March 5, 2010.
Edited by glass.dimly. Log in to edit this page.

This snippet adds <span class="breadcrumb-$number"></span> to each breadcrumb link. This is useful if you have a fixed number of breadcrumbs on certain pages and wish to customize, say, the second breadcrumb link.

Paste this into your template.php file. Make sure to remove the php tags and to replace yourthemename with, you guessed it, your theme name.

<?php
function yourthemename_breadcrumb($breadcrumb) {
  if (empty(
$breadcrumb)) {
    return
$themed_breadcrumb = '<div id="breadcrumb"></div>';
  }
  else  {
   
$themed_breadcrumb = '<div id="breadcrumb">';
   
$array_size = count($breadcrumb);
   
$i = 0;
    while (
$i < $array_size) {
     
$themed_breadcrumb .= '<span class="breadcrumb-';
     
$themed_breadcrumb .= $i;
     
$themed_breadcrumb .=  '">' . $breadcrumb[$i] . '</span>';
      if (
$i + 1 != $array_size ) {
       
$themed_breadcrumb .=  ' >> ';
      }
     
$i++;
    }
   
$themed_breadcrumb .= '</div>';
    return
$themed_breadcrumb;
  }
}
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

rickyd1’s picture

Thanks for getting me started with this. I need a first and last for my breadcrumbs. I modified the code to add a second class

If $1 is equal to 0 add my second class "first-crumb"

If $i + 1 is equal to the array count add second class "last-crumb"

I have it working on my system, but this is my first time sharing/modifying a snippet so please let me know if something is wrong with this version.

function phptemplate_breadcrumb($breadcrumb) {
  if (empty($breadcrumb)) {
    return $themed_breadcrumb = '<div id="breadcrumb"></div>';
  }
  else  {
    $themed_breadcrumb = '<div id="breadcrumb">';
    $array_size = count($breadcrumb);
    $i = 0;
    while ( $i < $array_size) {
      $themed_breadcrumb .= '<span class="breadcrumb-';
      $themed_breadcrumb .= $i;
      if ($i == 0) {
$themed_breadcrumb .= ' first-crumb';
      }
      if ($i+1 == $array_size) {
$themed_breadcrumb .= ' last-crumb';
      }
      $themed_breadcrumb .=  '">' . $breadcrumb[$i] . '</span>';
      if ( $i + 1 != $array_size ) {
        $themed_breadcrumb .=  ' >> ';
      }
      $i++;
    }
    $themed_breadcrumb .= '</div>';
    return $themed_breadcrumb;
  }
}
katiebot’s picture

<?php
function yourthemename_breadcrumb($breadcrumb) {
  if (empty(
$breadcrumb)) {
    return
$themed_breadcrumb = '<div id="breadcrumb"></div>';
  }
  else {
   
$themed_breadcrumb = '<div id="breadcrumb">';
   
$array_size = count($breadcrumb['breadcrumb']);
   
$i = 0;
    while (
$i < $array_size ) {
     
$themed_breadcrumb .= '<span class="breadcrumb-';
     
$themed_breadcrumb .= $i;
     
$themed_breadcrumb .=  '">' . $breadcrumb['breadcrumb'][$i] . '</span>';
      if (
$i + 1 != $array_size ) {
       
$themed_breadcrumb .=  ' › ';
      }
     
$i++;
    }
   
$themed_breadcrumb .= '</div>';
    return
$themed_breadcrumb;
  }
}
?>
candelas’s picture

I was trying in my template.php in the subtheme and it didn't work. I solve it with css

li {
    list-style: outside none none;
    margin-right: 10px;
    display: inline; 
}
li::before {
    content: " > ";
    padding-right: 10px;
}
li:first-child::before {
    content: "";
    padding-right: 0px;
}

I hope someone can use it :)

//trying to answer one question for each one that i make.
//this way, drupal will be more friendly and strong