• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Content Modeling for WordPress
    • WordPress Hidden Gems
    • Web Design
  • Work
    • MLIS Class Projects (2019-2022)
    • Portfolio (2002-2019)
    • Services
    • WordPress Plugins
    • WordPress Themes
    • Presentations and Interviews
    • on GitHub →

Stephanie Leary

Writer, WordPress consultant, recent MLIS grad

  • About
    • Press Kit
    • Presentations and Interviews
  • Contact Me

Absurdly simple PHP breadcrumbs

January 8, 2004 Stephanie Leary 14 Comments

[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&#91;$i&#93; . "/"; }
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.

Web Design

Reader Interactions

Comments

  1. Stephanie F says

    January 8, 2004 at 6:32 pm

    Beautiful!

    /me squirrels this away for use…

    Reply
  2. sergio says

    April 15, 2004 at 10:01 am

    Wonderful! Thanks for sharing this handy script!

    Reply
  3. Ingo says

    September 30, 2004 at 8:15 pm

    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.

    Reply
  4. Stephanie says

    September 30, 2004 at 8:26 pm

    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.

    Reply
  5. Praful says

    June 12, 2009 at 1:45 pm

    nice,simple and informative post

    Reply
  6. Nic says

    July 8, 2011 at 5:52 am

    Hi,

    Thanks for the script. I’ve got it to almost work. I have this folder structure:

    http://www.domain.com/folder1/folder2/page, from which I get:

    Home > Folder1 > Folder2

    However when I get to Home > Folder1 and then click on Folder1 the breadcrumbs displayed should be just Home, however I get:

    Home > with the backslash being a link to the home page.

    Any idea how I could fix this?

    Not sure if it matters, but I’m using RewriteRule in .htaccess.

    Many thanks.

    Reply
  7. Nic says

    July 8, 2011 at 6:35 am

    Oops, forgot to click the “notify me” box.

    Reply
  8. Nas says

    February 11, 2013 at 7:15 am

    Dont you have to escape the double quotes within double quotes:

    ” \” \” “

    Reply
    • Mark Middleton says

      February 19, 2014 at 4:11 pm

      @NAS You’re right-it won’t work without escaping the double quotes, or just use single quotes (apostrophes) inside the double quotes…

      Reply
  9. korting says

    March 31, 2013 at 4:00 pm

    I’m not sure exactly why but this weblog is loading incredibly slow for me. Is anyone else having this issue or is it a issue on my end? I’ll check back
    later on and see if the problem still exists.

    Reply
  10. Elliot Strickland says

    January 8, 2015 at 6:23 pm

    Is there a way to include the current page into the breadcrumbs list? Everything is working; except the current page does not show up.

    Reply
  11. Sarah Jay says

    November 22, 2016 at 5:03 am

    Really helpful script for me. Thanks for share that with us.

    Reply
  12. BestHolly says

    February 16, 2018 at 2:30 am

    I see you don’t monetize your website, don’t waste your
    traffic, you can earn additional cash every month because you’ve got high quality content.
    If you want to know how to make extra money,
    search for: Ercannou’s essential tools best adsense alternative

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Footer

My Books

I’m a front end developer at Equinox OLI, working on open source library software. I was previously a freelance WordPress developer in higher education. You can get in touch here or on LinkedIn.

Copyright © 2023 Stephanie Leary · Contact