Sillybean

January 8, 2004

Absurdly simple PHP breadcrumbs

[updated Dec. 8, 2007]

For my birthday, you get a gift of code. (Sorry, no returns.)

I’m not exactly a PHP guru, so a while back I borrowed a breadcrumb script from some download site. It did what I wanted — used the directory tree to create the crumb trail — but it required you to add a line to the script each and every time you added a directory to your site. In fact, I reviewed hundreds of breadcrumb scripts, and all of them were like this. Some went to the trouble of maintaining a database containing links and labels.

It’s ridiculous. I use readable directory names, and I want a script to take advantage of that.

I finally got tired of looking for one and wrote it. This code assumes that the link text will be the capitalized directory name, unless you specify an exception.

<?php
echo "<ul id="crumbs">";
/* get array containing each directory name in the path */
$parts = explode("/", dirname($_SERVER['REQUEST_URI']));  
echo "<li><a href="/">Home</a></li>";
foreach ($parts as $key => $dir) {
        switch ($dir) {
        case "about": $label = "About Us"; break;
        /* if not in the exception list above, 
            use the directory name, capitalized */
        default: $label = ucwords($dir); break;   
        }
        /* start fresh, then add each directory back to the URL */
        $url = "";
        for ($i = 1; $i <= $key; $i++) 
           { $url .= $parts[$i] . "/"; }
        if ($dir != "") 
           echo "<li>> <a href="/$url">$label</a></li>";
}
echo "</ul>";
?>

You’d just need to copy the case line to create more exceptions:

case "directoryname": $label = "Directory Name"; break;

(If you want to read up on the PHP used here before you monkey with it, the switch function is the essential part.)

Stash this code in a file called crumbs.php and then use one of the lines below to include it, preferably in your site’s master template. Nifty trick: if your pages are parsed for server-side includes but not for PHP, you can include the script the really old-fashioned way:

<!--#include virtual="/crumbs.php" -->

The crumbs will still work even though the main page is not PHP-based. Or you can always use PHP:

<?php include(/path/from/root/to/crumbs.php); ?>

I like my HTML nice and semantic, so the script spits out an unordered list of links. To make it look like a normal crumb trail, add this to your CSS:

#crumbs { list-style: none; }
#crumbs li { display: inline; }

For more on formatting lists, see Taming Lists.

Comments

  1. Beautiful!

    /me squirrels this away for use…

    Posted by Stephanie F on January 8th, 2004 at 6:32 pm

  2. Wonderful! Thanks for sharing this handy script!

    Posted by sergio on April 15th, 2004 at 10:01 am

  3. Hm, the Code looks really f…ed up!

    e.g. the & quot; should read ”

    Took me quite a while to figure it out. Ah’: and the download link is also broken for the txp-Plugin.

    Posted by Ingo on September 30th, 2004 at 8:15 pm

  4. Thanks, Ingo. Textpattern seems to have choked on this when I switched over from Movable Type. I think it’s fixed now.

    The plugin is another story—it needs some work before I put the link back up.

    Posted by Stephanie on September 30th, 2004 at 8:26 pm

Trackbacks & Pingbacks

  1. [...] http://www.sillybean.net/archives/452 [...]

    Pingback from SRJM.co.uk (Journal) : Guest : Journal Archive » Creating breadcrumb links - [ i ][ > ][...] : SRJM Design on October 27, 2007 at 3:37 am

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

'round here

Writing & Publishing 101

The Web Design for Authors series has evolved into Paged Media, a web design company devoted to authors

elsewhere

The recent troubles here evidently broke the LJ crossposter… which is now fixed, and has just spammed my flist with the last several posts. Sorry, LJ peeps.

Comment on this

Buy n Large — a link for people who have seen WALL-E

Comment on this

Text Prefs — a UK design firm is conducting a survey on web users’ text preferences. Tell them how you like it! They promise to publish the results when they’re done.

Comment on this

Fizzy Geography — should your character call it “soda,” “pop,” “Coke,” or something else? Depends which county they live in (or came from).

Comment on this

If you’re thinking of getting a new iPhone, this price comparison might be useful.

Comment on this

This week’s free Tor ebook is In the Garden of Iden, by Kage Baker. I love this book. If you haven’t read the Company series, here’s your opportunity for a free sample. Tor’s wallpapers this week are pretty great, too.

Comment on this

The Sock Obama — 100% pure hand-knitted offensiveness! And in breaking news elsewhere, Fox News is staffed entirely by racist morons. Oh, wait, we knew that.

Comment on this

Kucinich introducing articles of impeachment — he’s got 35 of ‘em, and he’s reading them all. Iraq, Iran, Valerie Plame, the attorney firings — it’s all there. I don’t think this is going to fly, but I love Kucinich to pieces for doing it. (Care to write your rep?)

Comment on this

Versions — Subversion client for Macs. I love it.

Comment on this

12-year-old environmentalist lays the smackdown on the UN — stuns adults into silence. “If you don’t know how to fix it, stop breaking it.”

Comment on this

Shadow Unit season finale in progress, one installment per night all week. It’s a nail-biter.

Comment on this