chrispooledotcom

Wordpress Breadcrumbs

01 January 2004

Reader beware: this article is a bit old!

This PhP script implements a breadcrumbs menu (see example below), which should work out of the box with Wordpress.

Example of breadcrumbs

<?php
/*
 * quick and dirty breadcrumb menu
 * initially designed for wordpress
 */
function CPbreadcrumbs() {
    $CPtheFullUrl = $_SERVER["REQUEST_URI"];
    $CPurlArray=explode("/",$CPtheFullUrl);
    echo '<a href="/">Home</a>';
    while (list($CPj,$CPtext) = each($CPurlArray)) {
        $CPdir='';
        if ($CPj > 1) {
            $CPi=1;
            while ($CPi < $CPj) {
                $CPdir .= '/' . $CPurlArray[$CPi];
                $CPtext = $CPurlArray[$CPi];
                $CPi++;
            }
            if($CPj < count($CPurlArray)-1) echo ' &raquo; <a href="'.$CPdir.'">' . str_replace("-", " ", $CPtext) . '</a>';
        }
    }
    echo wp_title();
}
CPbreadcrumbs();
?>

To use it, just put that block of code into your Wordpress file where you want the menu to appear.

If you don’t use Wordpress but still want to use this, you may have to change REQUEST_URI at the start of the script to PHP_SELF. And then at the bottom, change wp_title() to be a function that gives the title of the current page.