Php 简单动态面包屑

Php 简单动态面包屑,php,dynamic,breadcrumbs,Php,Dynamic,Breadcrumbs,我认为这个剧本对这里的所有noob都很有兴趣:)包括我:) 我要创建的是一个小代码,我可以在任何文件中使用,并将生成如下面包屑: 如果文件名为“website.com/templates/index.php”,则面包屑应显示: Website.com > Templates Website.com > Templates > Template Some Name ^^链接^^纯文本 如果文件名为“website.com/templates/template\u some\u

我认为这个剧本对这里的所有noob都很有兴趣:)包括我:)

我要创建的是一个小代码,我可以在任何文件中使用,并将生成如下面包屑:

如果文件名为“website.com/templates/index.php”,则面包屑应显示:

Website.com > Templates
Website.com > Templates > Template Some Name
^^链接^^纯文本

如果文件名为“website.com/templates/template\u some\u name.php”,则面包屑应显示:

Website.com > Templates
Website.com > Templates > Template Some Name

^^link^^^ link^^纯文本

Hmm,从您给出的示例来看,它看起来像“$\u SERVER['REQUEST\u URI']”,该函数可以帮助您。您可以使用explode将域名后面的URL拆分为一个数组,在每个正斜杠处将其分隔

作为一个非常基本的示例,可以实现如下内容:

$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
    echo ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
}
使用并在循环中输出结果:

$urlinfo = parse_url($the_url);
echo makelink($urlinfo['hostname']);
foreach($breadcrumb in $urlinfo['path']) {
  echo makelink($breadcrumb);
}

function makelink($str) {
  return '<a href="'.urlencode($str).'" title="'.htmlspecialchars($str).'">'.htmlspecialchars($str).'</a>';
}
$urlinfo=parse_url($u url);
echo makelink($urlinfo['hostname']);
foreach($urlinfo['path']中的面包屑){
echo makelink($breadcrumb);
}
函数makelink($str){
返回“”;
}

(伪代码)

对于一个简单的面包屑来说,这可能有些过分,但值得一试。我记得很早以前我刚开始的时候就有这个问题,但我从来没有真正解决过。也就是说,直到我现在才决定写这篇文章。:)

我已经尽我所能内联记录了3个可能的用例。享受吧!(如有任何问题,请随时提问)


嘿,多米尼克,你的回答很好,但是如果你有一个像“项目”这样的网站,链接会重复,因为它是$base的一部分,并且也会出现在$path数组中。因此,我调整并删除了$path数组中的第一项

//Trying to remove the first item in the array path so it doesn't repeat
array_shift($path);
我不知道这是否是最优雅的方式,但现在对我来说很有效

我在第13行的前面加上了代码

// Find out the index for the last value in our path array
$last = end(array_keys($path));

还使用RDFa编写了一个小脚本(您也可以使用微数据或其他格式) 此脚本还将记住您的站点结构

function breadcrumbs($text = 'You are here: ', $sep = ' &raquo; ', $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"><a href="'.$site.'" rel="v:url" property="v:title">'.$home.'</a>'.$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"><a href="'.$site.'" rel="v:url" property="v:title">'.$link.'</a>'.$sep.'</span>';
    $i++;
}
$bc .=  '</div>';
//Return the result
return $bc;}
函数面包屑($text='youre here:',$sep='»;,$home='home')){
//使用RDFa面包屑,也可用于微格式等。
$bc=''.$text;
//获取网站:
$site='http://'。$\u服务器['http\u主机];
//让所有变量跳过空变量
$crumps=array_filter(explode(“/”,$_SERVER[“REQUEST_URI”]);
//创建主面包屑
$bc.=''.$sep.'';
//清点所有非空面包屑
$nm=计数($crumps);
$i=1;
//把面包屑圈起来
foreach($crumps as$crump){
//使链接看起来漂亮
$link=ucfirst(str_替换(数组(“.php”、“-”、“”)、数组(“、”、“)、$crumb));
//松开最后一个分离器
$sep=$i==$nm?“”:$sep;
//把面包屑加到根上
$site.='/'.$crump;
//做下一个面包屑
$bc.=''.$sep.'';
$i++;
}
$bc.='';
//返回结果
返回$bc;}

我从多米尼克·巴恩斯(Dominic Barnes)的代码开始,结合了cWoDeR的反馈,当我使用子目录时,第三级的面包屑仍然存在问题。所以我重写了它,并包含了下面的代码

请注意,我已经设置了我的网站结构,以便将要从属于(链接自)根级别的页面的页面设置如下:

  • 创建一个与文件名(包括大写)完全相同的文件夹,减去后缀,作为根级别的文件夹

  • 将所有下级文件/页面放入此文件夹

(例如,如果需要Customers.php的页面:

  • 在Customers.php的同一级别创建名为Customers的文件夹

  • 将index.php文件添加到Customers文件夹中,该文件夹将重定向到该文件夹的调用页面(代码见下文)

此结构适用于多个级别的子文件夹

只需确保遵循上述文件结构并插入index.php文件,每个子文件夹中都显示了代码

每个子文件夹中index.php页面中的代码如下所示:

<!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>

重定向
如果您使用服务器的根目录作为web根目录(ie/var/www/html),则设置$root\u dir=“”:(不要将尾部“/”保留在中)。如果您使用网站的子目录(ie/var/www/html/web\u root),则设置$root\u dir=“web\u root/”;(将web\u root替换为web目录的实际名称)(确保包含尾部/)

无论如何,这是我的(衍生)代码:


这里有一个非常简单的动态面包屑(根据需要调整):


使用
explode()
函数的更好方法如下所示

别忘了替换超链接
href
中的URL变量

<?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 .= '/';
         }
     }
?>

这是我个人在我的网站上使用的代码。它是开箱即用的

<?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><a href="' . $root_domain . '" title="Home Page"><span>' . $home . '</span></a></li>';
    foreach ($breadcrumbs as $crumb) {
        $link = ucwords(str_replace(array(".php","-","_"), array(""," "," "), $crumb));
        $root_domain .=  $crumb . '/';
        $breadcrumb .= '<li><a href="'. $root_domain .'" title="'.$page_title.'"><span>' . $link . '</span></a></li>';
    }
    $breadcrumb .= '</ol>';
    $breadcrumb .= '</div>';
    $breadcrumb .= '</div>';
    return $breadcrumb;
}
echo breadcrumbs();
?>

您可以随意使用css填充和边距,直到您自己的站点正确使用为止。

这是我基于怀疑论者回答的解决方案。它从WordPress DB获得页面标题,而不是URL,因为存在拉丁字符问题(slug没有拉丁字符)。您还可以选择是否显示“主页”项目

/**
*展示面包屑
* 
*@param string | bool$home
*@param string$class
*@返回字符串
* 
*使用:echo breadcrumbs();
*/
函数面包屑($home='home',$class='items')){
$breadcrumb=“
    ”; $breadcrumbs=array_filter(分解('/',解析_url($_SERVER['REQUEST_URI'],PHP_url_PATH)); 如果($home){ $breadcrumb.='
  • '; } $path=''; foreach($breadcrumbs作为$crump){ $path.=$crumb.'/'; $page=按路径获取页面($path); 如果($home&($page->ID==get_选项('page_on_front')){ 继续; } $breadcrumb.='
  • '; } $breadcrumb.='
'; 返回$breadcrumb; }
使用:


贾兹达·娜·马克萨!
函数makeBreadCrumbs($separator='/')){
//提取uri路径
<?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><a href="' . $root_domain . '" title="Home Page"><span>' . $home . '</span></a></li>';
    foreach ($breadcrumbs as $crumb) {
        $link = ucwords(str_replace(array(".php","-","_"), array(""," "," "), $crumb));
        $root_domain .=  $crumb . '/';
        $breadcrumb .= '<li><a href="'. $root_domain .'" title="'.$page_title.'"><span>' . $link . '</span></a></li>';
    }
    $breadcrumb .= '</ol>';
    $breadcrumb .= '</div>';
    $breadcrumb .= '</div>';
    return $breadcrumb;
}
echo breadcrumbs();
?>
.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
}
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 ? '&raquo;' :'');

    $links .= "<a class=\"$clas\" href=\"$value\">$key</a> $sep";
    $count++;

endforeach;

return $links;