Community Documentation

adding classes to theme different parts of your breadcrumb

Last updated March 5, 2010. Created by glass.dimly on March 5, 2010.
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;
  }
}
?>

Comments

Thanks for this code

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;
  }
}

For Drupal 7

<?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;
  }
}
?>

About this page

Drupal version
Drupal 6.x
Audience
Designers/themers

Theming Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here