Lets say I have the following URL:
https://site.website/products/products-level-2
And on products-level-2 I have a breadcrumb hero section. What I'm expecting to see is:
Products / Products Level 2
However, with my current approach, I'm seeing:
/ Products / Products Level 2 /
^^ the above has the following HTML output:
<!-- link 1 - not needed -->
<a class="crumbMenu" href="site.website"> </a>
<span class="slash">/</span>
<!-- link 2 -->
<a class="crumbMenu" href="site.website//products">Products </a>
<span class="slash">/</span>
<!-- link 3 -->
<a class="crumbMenu" href="site.website//products/products-level-2">Products Level 2</a>
<span class="slash">/</span>
<!-- link 4 - not needed -->
<a class="crumbMenu" href="site.website//products/products-level-2"> </a>
<span class="slash">/</span>
The above has the following issues:
It's outputting blank / not needed anchor links.
In the href, it's outputting double // (i.e. site.website//products)
I also do not want the last item (i.e products level 2) to be a link (since the user is viewing the breadcrumb on that page).
I don't want / to appear at the start and end of the breadcrumb.
Here is my current approach:
<?php
// 1. Get URL
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
$address = $_SERVER['HTTP_HOST'];
// 2. Strip extras
$build = '';
foreach($crumbs as $crumb){
$build .= '/'.$crumb;
$crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
echo
"<a class='crumbMenu' href=".$address.$build.">".$crumb."</a>
<span class='slash'>/</span> ";
}
?>
To solve 1,2,4 just remove the leading and trailing slashes. To solve 3 you need to make a simple if:
// 1. Get URL
$crumbs = explode("/", trim($_SERVER["REQUEST_URI"], '/'));
$address = $_SERVER['HTTP_HOST'];
// 2. Strip extras
$build = '';
$lastKey = count($crumbs) - 1;
foreach($crumbs as $key => $crumb){
$build .= '/'.$crumb;
$crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
echo $key < $lastKey
? "<a class='crumbMenu' href=".$address.$build.">".$crumb."</a>
<span class='slash'>/</span>"
: $crumb;
}
Demo: https://3v4l.org/BqD7C
Solution:
The final working code you end up with is as follows:
<?php
// 1. Get URL
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
array_filter($crumbs);
$count = count($crumbs);
$address = $_SERVER['HTTP_HOST'];
// 2. Strip extras
$build = '';
$i = 0;
foreach($crumbs as $crumb) {
$href = (++$i != $count ? 'href="' . $address . $build . '"' : '');
$build .= '/'.$crumb;
$crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
echo
"<a class='crumbMenu' " . $href . ">".$crumb."</a>
<span class='slash'>/</span> ";
}
?>
Explanation:
1. It's outputting blank / not needed anchor links.
Simply run array_filter, this will remove any blank elements in the array.
array_filter($crumbs);
2. In the href, it's outputting double // (i.e. site.website//products)
The reason this happens is because it iterates through the blank element in the array first and as this adds a forward slash by default, by the second loop there is already a forward slash present, so it doubles up.
This issue is solved in the solution to question 1.
3. I also do not want the last item (i.e products level 2) to be a link (since the user is viewing the breadcrumb on that page).
After running array_filter on $crumbs you'll want to count how many elements are in the array using:
$count = count($crumbs);
Then when you are looping through the array, you'll want to workout when the loop is the last loop of the array:
$i = 0;
foreach($crumbs as $crumb) {
$href = (++$i != $count ? 'href="' . $address . $build . '"' : '');
// ...rest of foreach
$build .= '/' . $crumb;
$crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
echo "<a class='crumbMenu' " . $href . ">".$crumb."</a><span class='slash'>/</span> ";
}
4. I don't want / to appear at the start and end of the breadcrumb.
This was resolved with my solution to your first question right at the top of this answer.
/* A character used as a divider between the breadcrumbs */
$separator = ' / ';
/* The current page uri from which to build breadcrumb trail */
$uri='https://'.$_SERVER['HTTP_HOST'].'/products/computers/microsoft/laptops/acer/professional';
/* capture the path and explode to create an array - removing empty entries in the process */
$crumbs=array_filter( explode( '/', parse_url( $uri, PHP_URL_PATH ) ) );
/* The host and protocol for building correct links */
$host=sprintf( '%s://%s/', parse_url( $uri, PHP_URL_SCHEME ), parse_url( $uri, PHP_URL_HOST ) );
/* placeholder array to store generated links */
$html=[];
/* iterate through the array and build relevant HTML content - append to output array */
foreach( $crumbs as $index => $crumb ){
/* tweak the text displayed */
$crumb = ucfirst( str_replace( array( ".php", "_" ), array( "", " " ), $crumb ) );
/* the path should be all the previous breadcrumb crumbs */
$path=[];
for( $i=1; $i < $index; $i++ ) $path[]=$crumbs[ $i ];
$path=implode( DIRECTORY_SEPARATOR, $path );
/* create the link used in the html display */
$html[]= ( $index==count( $crumbs ) ) ? sprintf('<span>%s</span>', $crumb ) : sprintf("<a href='%s%s' class='crumbMenu'>%s</a>", $host, $path, $crumb );
}
echo $uri, "
<style>
#breadcrumbs{border:1px solid gray;padding:1rem;font-family:calibri,verdana,arial}
#breadcrumbs *{padding:1rem;}
#breadcrumbs a,
#breadcrumbs span:hover{color:blue}
#breadcrumbs a:hover{color:red;}
#breadcrumbs span{font-weight:bolder;}
</style>
<div id='breadcrumbs'>",
implode( $separator, $html ),
"</div>";
I have attached a breadcrumb and is now running. but how to tidy up the results in order to function.
this code
<?php
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
}
$urls = "http://example.com";
foreach($crumbs as $crumb){
$urls .= "/".$crumb;
echo '<li><a href="'.$urls.'">';
echo $crumb;
echo '</a></li>';
}
?>
and the result is
Home / / pages / contact
How to remove one slash after home?
and result for the url
http://example.com//pages/contact
i've tried to fix but still not solved
Add each 'breadcrumb' to an array in your loop, and then implode the array after the loop, adding the slashes.
$urls = "http://example.com";
$breadcrumbs = array();
foreach($crumbs as $crumb){
$breadcrumbs[] = "<li><a href='$urls/$crumb'>$crumb</a></li>";
}
echo '/'.implode('/',$breadcrumbs);
I found this useful PHP snippet and made a couple small modifications, but I have not been able to figure out how to exclude the home page from showing the trailing "»" after the word "Home". As a result, it ends up looking like this... Home » .
Is there a simple way to remove this without changing the way the breadcrumbs appear on any other page?
<?php
function breadcrumbs($sep = ' » ', $home = 'Home') {
//Use RDFa breadcrumb, can also be used for microformats etc.
$bc = '<div xmlns:v="http://rdf.data-vocabulary.org/#" id="crums">'.$text;
//Get the website:
$site = 'http://'.$_SERVER['HTTP_HOST'];
//Get all vars en skip the empty ones
$crumbs = array_filter( explode("/",$_SERVER["REQUEST_URI"]) );
//Create the home breadcrumb
$bc .= '<span typeof="v:Breadcrumb">'.$home.''.$sep.'</span>';
//Count all not empty breadcrumbs
$nm = count($crumbs);
$i = 1;
//Loop the crumbs
foreach($crumbs as $crumb){
//Make the link look nice
$link = ucfirst( str_replace( array(".php","-","_"), array(""," "," ") ,$crumb) );
//Loose the last seperator
$sep = $i==$nm?'':$sep;
//Add crumbs to the root
$site .= '/'.$crumb;
//Make the next crumb
$bc .= '<span typeof="v:Breadcrumb">'.$link.''.$sep.'</span>';
$i++;
}
$bc .= '</div>';
//Return the result
return $bc;}
?>
<p><?= breadcrumbs() ?></p>
You have to remove the separator character that is put each time after "Home". Add it only if there is something after "Home".
$crumbs = array_filter( explode("/",$_SERVER["REQUEST_URI"]) );
//Count all not empty breadcrumbs
$nm = count($crumbs);
$i = 1;
// Add first separator if there is at least one crumb
$homesep = $nm == 0?'':$sep;
//Create the home breadcrumb
$bc .= '<span typeof="v:Breadcrumb">'.$home.''.$homesep.'</span>';
//Loop the crumbs
foreach($crumbs as $crumb){
...
}
I think this script is of big interest to any noob around here :) including me :)
What I want to create is a little code that I can use in any file and will generate a breadcrumb like this:
If the file is called "website.com/templates/index.php" the breadcrumb should show:
Website.com > Templates
^^ link ^^plain text
If the file is called "website.com/templates/template_some_name.php" the breadcrumb should show:
Website.com > Templates > Template Some Name
^^ link ^^link ^^plain text
This may be overkill for a simple breadcrumb, but it's worth a shot. I remember having this issue a long time ago when I first started, but I never really solved it. That is, until I just decided to write this up now. :)
I have documented as best I can inline, at the bottom are 3 possible use cases. Enjoy! (feel free to ask any questions you may have)
<?php
// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' » ', $home = 'Home') {
// This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
$path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
// This will build our "base URL" ... Also accounts for HTTPS :)
$base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
// Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
$breadcrumbs = Array("$home");
// Find out the index for the last value in our path array
$last = end(array_keys($path));
// Build the rest of the breadcrumbs
foreach ($path AS $x => $crumb) {
// Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
$title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
// If we are not on the last index, then display an <a> tag
if ($x != $last)
$breadcrumbs[] = "$title";
// Otherwise, just display the title (minus)
else
$breadcrumbs[] = $title;
}
// Build our temporary array (pieces of bread) into one big string :)
return implode($separator, $breadcrumbs);
}
?>
<p><?= breadcrumbs() ?></p>
<p><?= breadcrumbs(' > ') ?></p>
<p><?= breadcrumbs(' ^^ ', 'Index') ?></p>
Hmm, from the examples you gave it seems like "$_SERVER['REQUEST_URI']" and the explode() function could help you. You could use explode to break up the URL following the domain name into an array, separating it at each forward-slash.
As a very basic example, something like this could be implemented:
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
echo ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
}
Also made a little script using RDFa (you can also use microdata or other formats) Check it out on google
This script also keeps in mind your site structure.
function breadcrumbs($text = 'You are here: ', $sep = ' » ', $home = 'Home') {
//Use RDFa breadcrumb, can also be used for microformats etc.
$bc = '<div xmlns:v="http://rdf.data-vocabulary.org/#" id="crums">'.$text;
//Get the website:
$site = 'http://'.$_SERVER['HTTP_HOST'];
//Get all vars en skip the empty ones
$crumbs = array_filter( explode("/",$_SERVER["REQUEST_URI"]) );
//Create the home breadcrumb
$bc .= '<span typeof="v:Breadcrumb">'.$home.''.$sep.'</span>';
//Count all not empty breadcrumbs
$nm = count($crumbs);
$i = 1;
//Loop the crumbs
foreach($crumbs as $crumb){
//Make the link look nice
$link = ucfirst( str_replace( array(".php","-","_"), array(""," "," ") ,$crumb) );
//Loose the last seperator
$sep = $i==$nm?'':$sep;
//Add crumbs to the root
$site .= '/'.$crumb;
//Make the next crumb
$bc .= '<span typeof="v:Breadcrumb">'.$link.''.$sep.'</span>';
$i++;
}
$bc .= '</div>';
//Return the result
return $bc;}
use parse_url and then output the result in a loop:
$urlinfo = parse_url($the_url);
echo makelink($urlinfo['hostname']);
foreach($breadcrumb in $urlinfo['path']) {
echo makelink($breadcrumb);
}
function makelink($str) {
return ''.htmlspecialchars($str).'';
}
(pseudocode)
I started with the code from Dominic Barnes, incorporated the feedback from cWoDeR and I still had problems with the breadcrumbs at the third level when I used a sub-directory. So I rewrote it and have included the code below.
Note that I have set up my web site structure such that pages to be subordinate to (linked from) a page at the root level are set up as follows:
Create a folder with the EXACT same name as the file (including capitalization), minus the suffix, as a folder at the root level
place all subordinate files/pages into this folder
(eg, if want sobordinate pages for Customers.php:
create a folder called Customers at the same level as Customers.php
add an index.php file into the Customers folder which redirects to the calling page for the folder (see below for code)
This structure will work for multiple levels of subfolders.
Just make sure you follow the file structure described above AND insert an index.php file with the code shown in each subfolder.
The code in the index.php page in each subfolder looks like:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Redirected</title>
</head>
<body>
<?php
$root_dir = "web_root/" ;
$last_dir=array_slice(array_filter(explode('/',$_SERVER['PHP_SELF'])),-2,1,false) ;
$path_to_redirect = "/".$root_dir.$last_dir[0].".php" ;
header('Location: '.$path_to_redirect) ;
?>
</body>
</html>
If you use the root directory of the server as your web root (ie /var/www/html) then set $root_dir="": (do NOT leave the trailing "/" in). If you use a subdirectory for your web site (ie /var/www/html/web_root then set $root_dir = "web_root/"; (replace web_root with the actual name of your web directory)(make sure to include the trailing /)
at any rate, here is my (derivative) code:
<?php
// Big Thank You to the folks on StackOverflow
// See http://stackoverflow.com/questions/2594211/php-simple-dynamic-breadcrumb
// Edited to enable using subdirectories to /var/www/html as root
// eg, using /var/www/html/<this folder> as the root directory for this web site
// To enable this, enter the name of the subdirectory being used as web root
// in the $directory2 variable below
// Make sure to include the trailing "/" at the end of the directory name
// eg use $directory2="this_folder/" ;
// do NOT use $directory2="this_folder" ;
// If you actually ARE using /var/www/html as the root directory,
// just set $directory2 = "" (blank)
// with NO trailing "/"
// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' » ' , $home = 'Home')
{
// This sets the subdirectory as web_root (If you want to use a subdirectory)
// If you do not use a web_root subdirectory, set $directory2=""; (NO trailing /)
$directory2 = "web_root/" ;
// This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ;
$path_array = array_filter(explode('/',$path)) ;
// This line of code accommodates using a subfolder (/var/www/html/<this folder>) as root
// This removes the first item in the array path so it doesn't repeat
if ($directory2 != "")
{
array_shift($path_array) ;
}
// This will build our "base URL" ... Also accounts for HTTPS :)
$base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/'. $directory2 ;
// Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
$breadcrumbs = Array("$home") ;
// Get the index for the last value in our path array
$last = end($path_array) ;
// Initialize the counter
$crumb_counter = 2 ;
// Build the rest of the breadcrumbs
foreach ($path_array as $crumb)
{
// Our "title" is the text that will be displayed representing the filename without the .suffix
// If there is no "." in the crumb, it is a directory
if (strpos($crumb,".") == false)
{
$title = $crumb ;
}
else
{
$title = substr($crumb,0,strpos($crumb,".")) ;
}
// If we are not on the last index, then create a hyperlink
if ($crumb != $last)
{
$calling_page_array = array_slice(array_values(array_filter(explode('/',$path))),0,$crumb_counter,false) ;
$calling_page_path = "/".implode('/',$calling_page_array).".php" ;
$breadcrumbs[] = "".$title."" ;
}
// Otherwise, just display the title
else
{
$breadcrumbs[] = $title ;
}
$crumb_counter = $crumb_counter + 1 ;
}
// Build our temporary array (pieces of bread) into one big string :)
return implode($separator, $breadcrumbs) ;
}
// <p><?= breadcrumbs() ? ></p>
// <p><?= breadcrumbs(' > ') ? ></p>
// <p><?= breadcrumbs(' ^^ ', 'Index') ? ></p>
?>
hey dominic your answer was nice but if your have a site like http://localhost/project/index.php the 'project' link gets repeated since it's part of $base and also appears in the $path array. So I tweaked and removed the first item in the $path array.
//Trying to remove the first item in the array path so it doesn't repeat
array_shift($path);
I dont know if that is the most elegant way, but it now works for me.
I add that code before this one on line 13 or something
// Find out the index for the last value in our path array
$last = end(array_keys($path));
Here is a great simple dynamic breadcrumb (tweak as needed):
<?php
$docroot = "/zen/index5.php";
$path =($_SERVER['REQUEST_URI']);
$names = explode("/", $path);
$trimnames = array_slice($names, 1, -1);
$length = count($trimnames)-1;
$fixme = array(".php","-","myname");
$fixes = array(""," ","My<strong>Name</strong>");
echo '<div id="breadwrap"><ol id="breadcrumb">';
$url = "";
for ($i = 0; $i <= $length;$i++){
$url .= $trimnames[$i]."/";
if($i>0 && $i!=$length){
echo '<li>'.ucfirst(str_replace($fixme,$fixes,$trimnames[$i]) . ' ').'</li>';
}
elseif ($i == $length){
echo '<li class="current">'.ucfirst(str_replace($fixme,$fixes,$trimnames[$i]) . ' ').'</li>';
}
else{
echo $trimnames[$i]='<li><span> </span></li>';
}
}
echo '</ol>';
?>
A better one using explode() function is as follows...
Don't forget to replace your URL variable in the hyperlink href.
<?php
if($url != ''){
$b = '';
$links = explode('/',rtrim($url,'/'));
foreach($links as $l){
$b .= $l;
if($url == $b){
echo $l;
}else{
echo "<a href='URL?url=".$b."'>".$l."/</a>";
}
$b .= '/';
}
}
?>
Here is my solution based on Skeptic answer. It gets page title from WordPress DB, not from URL because there is a problem with latin characters (slug doesn't has a latin characters). You can also choose to display "home" item or not.
/**
* Show Breadcrumbs
*
* #param string|bool $home
* #param string $class
* #return string
*
* Using: echo breadcrumbs();
*/
function breadcrumbs($home = 'Home', $class = 'items') {
$breadcrumb = '<ul class="'. $class .'">';
$breadcrumbs = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
if ($home) {
$breadcrumb .= '<li>' . $home . '</li>';
}
$path = '';
foreach ($breadcrumbs as $crumb) {
$path .= $crumb . '/';
$page = get_page_by_path($path);
if ($home && ($page->ID == get_option('page_on_front'))) {
continue;
}
$breadcrumb .= '<li>' . $page->post_title . '</li>';
}
$breadcrumb .= '</ul>';
return $breadcrumb;
}
Using:
<div class="breadcrumb">
<div class="container">
<h3 class="breadcrumb__title">Jazda na maxa!</h3>
<?php echo breadcrumbs('Start', 'breadcrumb__items'); ?>
</div>
</div>
This is the code that i personally use in my sites. Works outside of the box.
<?php
function breadcrumbs($home = 'Home') {
global $page_title; //global varable that takes it's value from the page that breadcrubs will appear on. Can be deleted if you wish, but if you delete it, delete also the title tage inside the <li> tag inside the foreach loop.
$breadcrumb = '<div class="breadcrumb-container"><div class="container"><ol class="breadcrumb">';
$root_domain = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'].'/';
$breadcrumbs = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
$breadcrumb .= '<li><i class="fa fa-home"></i><span>' . $home . '</span></li>';
foreach ($breadcrumbs as $crumb) {
$link = ucwords(str_replace(array(".php","-","_"), array(""," "," "), $crumb));
$root_domain .= $crumb . '/';
$breadcrumb .= '<li><span>' . $link . '</span></li>';
}
$breadcrumb .= '</ol>';
$breadcrumb .= '</div>';
$breadcrumb .= '</div>';
return $breadcrumb;
}
echo breadcrumbs();
?>
The css:
.breadcrumb-container {
width: 100%;
background-color: #f8f8f8;
border-bottom-color: 1px solid #f4f4f4;
list-style: none;
margin-top: 72px;
min-height: 25px;
box-shadow: 0 3px 0 rgba(60, 57, 57, .2)
}
.breadcrumb-container li {
display: inline
}
.breadcrumb {
font-size: 12px;
padding-top: 3px
}
.breadcrumb>li:last-child:after {
content: none
}
.breadcrumb>li:last-child {
font-weight: 700;
font-style: italic
}
.breadcrumb>li>i {
margin-right: 3px
}
.breadcrumb>li:after {
font-family: FontAwesome;
content: "\f101";
font-size: 11px;
margin-left: 3px;
margin-right: 3px
}
.breadcrumb>li+li:before {
font-size: 11px;
padding-left: 3px
}
Feel free to play around with the css padding and margins until you get it right for your own site.
function makeBreadCrumbs($separator = '/'){
//extract uri path parts into an array
$breadcrumbs = array_filter(explode('/',parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
//determine the base url or domain
$base = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
$last = end($breadcrumbs); //obtain the last piece of the path parts
$crumbs['Home'] = $base; //Our first crumb is the base url
$current = $crumbs['Home']; //set the current breadcrumb to base url
//create valid urls from the breadcrumbs and store them in an array
foreach ($breadcrumbs as $key => $piece) {
//ignore file names and create urls from directory path
if( strstr($last,'.php') == false){
$current = $current.$separator.$piece;
$crumbs[$piece] =$current;
}else{
if($piece !== $last){
$current = $current.$separator.$piece;
$crumbs[$piece] =$current;
}
}
}
$links = '';
$count = 0;
//create html tags for displaying the breadcrumbs
foreach ($crumbs as $key => $value) :
$x = array_filter(explode('/',parse_url($value, PHP_URL_PATH)));
$last = end($x);
//this will add a class to the last link to control its appearance
$clas = ($count === count($crumbs) -1 ? ' current-crumb' : '' );
//determine where to print separators
$sep = ( $count > -1 && $count < count($crumbs) -1 ? '»' :'');
$links .= "<a class=\"$clas\" href=\"$value\">$key</a> $sep";
$count++;
endforeach;
return $links;
}
I am trying to add drop down menus to a drupal theme which uses text sliding door CSS rounding.
The current version uses a primary links injection of the span into the a tags, which works fine. But doesn't support drop down menus.
Working code:
<?php print theme('links', $primary_links, array('class' => 'links primary-links')) ?>
In the template with a template.php file addition:
<?php
// function for injecting spans inside anchors which we need for the theme's rounded corner background images
function strands_guybrush_links($links, $attributes = array('class' => 'links')) {
$output = '';
if (count($links) > 0) {
$output = '<ul'. drupal_attributes($attributes) .'>';
$num_links = count($links);
$i = 1;
foreach ($links as $key => $link) {
$class = $key;
// Add first, last and active classes to the list of links to help out themers.
if ($i == 1) {
$class .= ' first';
}
if ($i == $num_links) {
$class .= ' last';
}
if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
$class .= ' active';
}
$output .= '<li'. drupal_attributes(array('class' => $class)) .'>';
if (isset($link['href'])) {
$link['title'] = '<span class="link">' . check_plain($link['title']) . '</span>';
$link['html'] = TRUE;
// Pass in $link as $options, they share the same keys.
$output .= l($link['title'], $link['href'], $link);
}
else if (!empty($link['title'])) {
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
$span_attributes = '';
if (isset($link['attributes'])) {
$span_attributes = drupal_attributes($link['attributes']);
}
$output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
}
$i++;
$output .= "</li>\n";
}
$output .= '</ul>';
}
return $output;
}
?>
So I have added the Nice Menu module which works well and allows the drop down menu functions for my navigation which is now addressed from the template using:
<?php print theme_nice_menu_primary_links() ?>
The issue is that the a tags need to have spans inside to allow for the selected state markup. I have tried every angle I could find to edit the drupal function menu_item_link which is used by nice menus to build the links.
E.g. I looked at the drupal forum for two days and no joy.
The lines in the module that build the links are:
function theme_nice_menu_build($menu) {
$output = '';
// Find the active trail and pull out the menus ids.
menu_set_active_menu_name('primary-links');
$trail = menu_get_active_trail('primary-links');
foreach ($trail as $item) {
$trail_ids[] = $item['mlid'];
}
foreach ($menu as $menu_item) {
$mlid = $menu_item['link']['mlid'];
// Check to see if it is a visible menu item.
if ($menu_item['link']['hidden'] == 0) {
// Build class name based on menu path
// e.g. to give each menu item individual style.
// Strip funny symbols.
$clean_path = str_replace(array('http://', '<', '>', '&', '=', '?', ':'), '', $menu_item['link']['href']);
// Convert slashes to dashes.
$clean_path = str_replace('/', '-', $clean_path);
$class = 'menu-path-'. $clean_path;
$class .= in_array($mlid, $trail_ids) ? ' active' : '';
// If it has children build a nice little tree under it.
if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
// Keep passing children into the function 'til we get them all.
$children = theme('nice_menu_build', $menu_item['below']);
// Set the class to parent only of children are displayed.
$class .= $children ? ' menuparent ' : '';
// Add an expanded class for items in the menu trail.
$output .= '<li id="menu-'. $mlid .'" class="'. $class .'">'. theme('menu_item_link', $menu_item['link']);
// Build the child UL only if children are displayed for the user.
if ($children) {
$output .= '<ul>';
$output .= $children;
$output .= "</ul>\n";
}
$output .= "</li>\n";
}
else {
$output .= '<li id="menu-'. $mlid .'" class="'. $class .'">'. theme('menu_item_link', $menu_item['link']) .'</li>'."\n";
}
}
}
return $output;
}
As you can see the $output uses menu_item_link to parse the array into links and to added the class of active to the selected navigation link.
The question is how do I add a span inside the a tags OR how do I wrap the a tags with a span that has the active class to style the sliding door links?
If you want to wrap the a tags with a span, you can overwrite the theme_nice_menu_build and add your span to the output. If you want to inside the a tag you need to overwrite the menu_item_link.
You can overwrite a theme funciton by creation a function call your_theme_name_function_name and Drupal will use that function to render the markup instead of the default one. That way you can alter the markup any way you want. This function should be in your theme's template.php file.
A good way to start is to copy the function you want to overwrite and just alter to your likings.
A lot has happened since Drupal 4.7, I don't hope you use that. It's quite easy to insert span tags:
function your_theme_name_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
$link['localized_options']['html'] = TRUE;
return l('<span>' . $link['title'] . '</span>', $link['href'], $link['localized_options']);
}
I tested this and it works just fine.