修复我的breadcrumbs.php文件-它忽略了部分URL
我希望使用这个breadcrumb脚本生成指向我站点上父URL的链接 我成功地将其链接到距主页只有一次点击距离的页面上,但是对于已经深入网站一步的URL,它不起作用 以下是代码,您可以查看:修复我的breadcrumbs.php文件-它忽略了部分URL,php,wordpress,Php,Wordpress,我希望使用这个breadcrumb脚本生成指向我站点上父URL的链接 我成功地将其链接到距主页只有一次点击距离的页面上,但是对于已经深入网站一步的URL,它不起作用 以下是代码,您可以查看: <?php if( $breadcrumbs === TRUE ): ?> <?php // This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the
<?php if( $breadcrumbs === TRUE ): ?>
<?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("<a href=\"$base\">$home</a>");
// 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('.html', '.php', '_', '-'), Array('', '', ' ', ' '), $crumb));
// If we are not on the last index, then display an <a> tag
if ($x != $last)
$breadcrumbs[] = "<a href=\"$base$crumb.html\">$title</a>";
// 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 id="breadcrumbs" class="breadcrumbs"><?= breadcrumbs(' > ') ?></p>
<?php endif; ?>
这里的主要问题是,您需要以不同的方式对待链接文本和链接href。对于文本,您只想为my/foo/bar/baz.html
示例生成的第二个链接输出当前路径段的名称-bar
;但是href中的路径不仅必须是bar
(或bar.html
),而且前面的段也需要保留在这里。使用helper变量最简单,它初始化为空字符串,然后在每次迭代中追加/
和当前路径段。(通过将斜杠放在第一位,您仍然可以在末尾添加.html
;如果您以相反的方式添加,并且您的助手变量包含foo/bar/
,您将得到错误的路径foo/bar/.html
)
由于此相对路径已经以/
开头,因此您应该从$base URI中删除尾随的/
,以避免在此处出现双斜杠-或者完全保留该基本URI,以/
开头的相对链接始终指向域根
未经测试,但大致如下:
// Build the rest of the breadcrumbs
// helper variable to assemble the path, initialized as empty string
$tempPath = '';
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('.html', '.php', '_', '-'), Array('', '', ' ', ' '), $crumb));
// append / and current path segment
$tempPath .= '/' . $crumb;
// If we are not on the last index, then display an <a> tag
if ($x != $last)
// insert this temporary path into link href, and add .html
$breadcrumbs[] = "<a href=\"$tempPath.html\">$title</a>";
// Otherwise, just display the title (minus)
else
$breadcrumbs[] = $title;
}
//构建剩余的面包屑
//用于组装路径的helper变量,初始化为空字符串
$tempPath='';
foreach($x=>$crump的路径){
//我们的“标题”是将要显示的文本(去掉.php并将“\u1”变成一个空格)
$title=ucwords(str_replace(数组('.html'、'.php'、''、'-')、数组(''、''、'')、$crumb));
//追加/删除当前路径段
$tempPath.='/'.$crump;
//如果我们不在最后一个索引上,则显示“;
//否则,只显示标题(减号)
其他的
$breadcrumbs[]=$title;
}
$crumb
仅为各自的路径段,您正在链接中输出该路径段。您需要将之前连接的路径段存储在一个帮助器变量中。@misorude-很高兴再次收到您的来信!感谢您的参与。我将研究如何实现该路径段。您能向我展示的任何其他指针或示例?Thanks,Jason你实际的URL结构是什么?只是想知道,因为你在这里的末尾附加了.html
。如果你有一个/foo/bar/baz.html
,这是否意味着/foo/bar.html
和/foo.html
也存在?否则,你不能将.html
附加到面包屑路径中的所有链接,但需要区分这些情况。@misorude-面包屑中的所有页面都是可链接的。我已将其设置为父页面。其中所有页面都有.html扩展名(如果它是面包屑路径中的最后一个).So/foo/bar/baz.html、/foo/bar.html和/foo.html都存在。只是确认一下。它确实有效。非常感谢你,我真的理解你所做的-所以感谢你教得太完美了:-)很乐意帮助。