根据面包屑级别动态获取页面 link
Dynamically get page link based on breadcrumb level
我正在尝试通过 PHP 创建一个面包屑菜单,目前有以下内容:
<?php
// 1. Get URL
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
$address = 'http://'.$_SERVER['HTTP_HOST'];
// 2. Strip extras
foreach($crumbs as $crumb){
$crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
echo "<a href=".$address.">"."<span class='crumbMenu'>".$crumb."</span></a>";
}
?>
假设我有以下页面层次结构:Products > Products Level 2 > Products Level 3
上面的代码会吐出:
Products
Products Level 2
Products Level 3
这是正确的。但是,link 不是。
阅读 后,我确定我的方法是错误的,但不确定我可以采取什么其他方法来动态获取每个面包屑项目 link?
我得到的链接:
localhost:8080
我期待的链接:
产品:/products
产品级别 2:/products/products-level-2
产品级别 3:/products/products-level-2/products-level-3
您似乎忘记了将路由添加到 $address
变量,所以您所有的面包屑都指向基本服务器地址。尝试以下操作:
<?php
// 1. Get URL
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
$address = 'http://'.$_SERVER['HTTP_HOST'];
// 2. Strip extras
$build = $address.'/products';
foreach($crumbs as $crumb){
if(in_array($crumb, [$_SERVER['HTTP_HOST'], 'products'])) {
continue;
}
$build .= '/'.$crumb;
$crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
echo "<a href=".$address.$build.">"."<span class='crumbMenu'>".$crumb."</span></a>";
}
?>
很难准备好你的回声,这没有帮助。我创建了一个小函数,它应该可以满足您的要求。随意修改它以满足您的需求。
$crumbs = "/x/y/z";
$address = "localhost:8080";
$crumbs = explode('/', $crumbs);
$end = '';
$href = '';
foreach ($crumbs as $crumb) {
$crumb = str_replace('.php', '', $crumb);
$end .= $crumb . ' ';
$href .= $crumb . '/';
}
echo("HREF => " . $href);
echo("\n");
echo("TITLE => " . $end);
我正在尝试通过 PHP 创建一个面包屑菜单,目前有以下内容:
<?php
// 1. Get URL
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
$address = 'http://'.$_SERVER['HTTP_HOST'];
// 2. Strip extras
foreach($crumbs as $crumb){
$crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
echo "<a href=".$address.">"."<span class='crumbMenu'>".$crumb."</span></a>";
}
?>
假设我有以下页面层次结构:Products > Products Level 2 > Products Level 3
上面的代码会吐出:
Products
Products Level 2
Products Level 3
这是正确的。但是,link 不是。
阅读 后,我确定我的方法是错误的,但不确定我可以采取什么其他方法来动态获取每个面包屑项目 link?
我得到的链接:
localhost:8080
我期待的链接:
产品:
/products
产品级别 2:
/products/products-level-2
产品级别 3:
/products/products-level-2/products-level-3
您似乎忘记了将路由添加到 $address
变量,所以您所有的面包屑都指向基本服务器地址。尝试以下操作:
<?php
// 1. Get URL
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
$address = 'http://'.$_SERVER['HTTP_HOST'];
// 2. Strip extras
$build = $address.'/products';
foreach($crumbs as $crumb){
if(in_array($crumb, [$_SERVER['HTTP_HOST'], 'products'])) {
continue;
}
$build .= '/'.$crumb;
$crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
echo "<a href=".$address.$build.">"."<span class='crumbMenu'>".$crumb."</span></a>";
}
?>
很难准备好你的回声,这没有帮助。我创建了一个小函数,它应该可以满足您的要求。随意修改它以满足您的需求。
$crumbs = "/x/y/z";
$address = "localhost:8080";
$crumbs = explode('/', $crumbs);
$end = '';
$href = '';
foreach ($crumbs as $crumb) {
$crumb = str_replace('.php', '', $crumb);
$end .= $crumb . ' ';
$href .= $crumb . '/';
}
echo("HREF => " . $href);
echo("\n");
echo("TITLE => " . $end);