Use your left/right keys to browse tutorials
Display breadcrumbs on your site using PHP

Display breadcrumbs on your site using PHP

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on August 17, 2011

When you have a fully dynamic site it’s useful to show the user where they are by breaking down the URL structure so they can navigate backwards through the site. For example this url: http://papermashup.com/categories/jquery/ shows that ‘jquery’ is part of ‘categories’ so the user could navigate backwards through the url structure.

The Code

The first line of the function gets the REQUEST_URI (/path/to/file.php), splits the string (using ‘/’) into an array, then filters out any empty values. We then store the base url for the site in $base_url. Next we find the array key for the last value in the $path array. We then do a for loop to build up the breadcrumbs and determine where to put the a tag etc.


function breadcrumbs($separator = ' » ', $home = 'Home') {
   
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
    $base_url = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
    $breadcrumbs = array("<a href=\"$base_url\">$home</a>");

    $last = end(array_keys($path));

    foreach ($path AS $x => $crumb) {
        $title = ucwords(str_replace(array('.php', '_'), Array('', ' '), $crumb));
        if ($x != $last){
            $breadcrumbs[] = '<a href="$base_url$crumb">$title</a>';
        }else{
            $breadcrumbs[] = $title; 
		}
    }

    return implode($separator, $breadcrumbs);
}	

Calling the function

This is the simple bit! Simply add the above function to the top of your PHP page and call the function as shown below. You can optionally add a separator symbol or specify the wording for ‘Home’.


echo breadcrumbs();



  • priti

    this code is for two level…plz give me idea for multilevel

  • http://Kryptologist.net Vitaly Van Deusen

    I found some errors that caused to not work. You put ‘Array’ instead of ‘array’ on the 2nd parameter of str_replace. It was replacing anything, just giving me Home / Array. You also don’t need parse_url() method either. My website is being worked on constantly. I would appreciate it if some one came to look at it other than just me.

    Here is the rewritten function that goes well with Bootstrap CSS:

    function breadcrumbs($separator = ‘ / ‘, $home = ‘Home’) {

    $path = array_filter(explode(‘/’, $_SERVER['REQUEST_URI']));
    $base_url = ($_SERVER['HTTPS'] ? ‘https’ : ‘http’) . ‘://’ . $_SERVER['HTTP_HOST'] . ‘/’;
    $breadcrumbs = array(‘‘.$home.’‘);

    $last =end(array_keys($path));

    foreach ($path as $x => $crumb) {
    $title = ucwords(str_replace(array(‘.php’, ‘_’, ‘-’), array(”, ‘ ‘, ‘ ‘), $crumb));
    if ($x!= $last){
    $breadcrumbs[] = ‘‘.$title.’‘;
    }
    else{
    $breadcrumbs[] = ”.$title.”;
    }
    }

    return implode($separator, $breadcrumbs);
    }

    • http://Kryptologist.net Vitaly Van Deusen

      Ugh.. <a> got turned into actual links.

      $breadcrumbs = array(‘<a href=\”‘.$base_url.’\”>’.$home.’</a>’);

      if ($x!= $last){
      $breadcrumbs[] = ‘<li><a href=”‘.$base_url.$crumb.’”>’.$title.’</a></li>';
      }
      else{
      $breadcrumbs[] = '<li class="active">'.$title.'</li>';
      }

  • http://www.computeraben.dk Marcus

    Hi
    Very interesting
    Is it possible to create a php code that can show the breadcrumb text within the title tag in Joomla. Something like “title print breadcrumb text /title”
    best regards