简单的动态面包屑
我认为这个脚本对这里的任何菜鸟都很感兴趣:)包括我:)
我想要创建的是一些可以在任何文件中使用的小代码,并将生成如下所示的面包屑:
如果该文件名为“< em>website.com/templates/index.php”面包屑应显示:
Website.com > Templates
^^ 链接 ; ^^纯文本
如果文件名为“website.com/templates/template_some_name.php”,面包屑应显示:
Website.com > Templates > Template Some Name
^ ^ 链接 ^^链接 &n公共服务提供商; ^^纯文本
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对于简单的面包屑来说,这可能有点过分了,但值得一试。我记得很久以前刚开始的时候就遇到过这个问题,但一直没有真正解决。也就是说,直到我决定现在写下这篇文章。 :)
我已经尽可能内联地记录了,底部是 3 个可能的用例。享受! (如有任何问题,请随时提出)
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)
嗯,从您给出的示例来看,它看起来像“$_SERVER['REQUEST_URI']”和 explode() 函数可以帮助你。您可以使用explode 将域名后面的URL 分解为一个数组,并在每个正斜杠处将其分隔开。
作为一个非常基本的例子,可以实现这样的事情:
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:
还使用 RDFa 制作了一个小脚本(您也可以使用微数据或其他格式)在 google 上查看
该脚本还会记住您的站点结构。
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.
使用
parse_url
然后循环输出结果:(伪代码)
use
parse_url
and then output the result in a loop:(pseudocode)
我从 Dominic Barnes 的代码开始,结合了 cWoDeR 的反馈,当我使用子目录时,我在第三层的面包屑中仍然遇到问题。所以我重写了它并包含了下面的代码。
请注意,我已经设置了我的网站结构,以便从属于(链接自)根级别页面的页面设置如下:
创建一个与文件名称完全相同的文件夹(包括大小写) ),减去后缀,作为根级别的文件夹
放置所有下级文件夹文件/页面放入此文件夹
(例如,如果需要 Customers.php 的附属页面:
创建一个名为 Customers 的文件夹,与 Customers.php 处于同一级别
将一个 index.php 文件添加到 Customers 文件夹中,该文件重定向到该文件夹的调用页面(请参阅下面的代码)
此结构适用于多层子文件夹。
只需确保遵循上述文件结构,并插入一个包含每个子文件夹中显示的代码的 index.php 文件。
每个子文件夹中的index.php页面中的代码如下所示:
如果您使用服务器的根目录作为您的Web根(即/var/www/html),则设置$root_dir =“”:(不要留下尾随“/“ 在)。如果您的网站使用子目录(即 /var/www/html/web_root,则设置 $root_dir = "web_root/"; (将 web_root 替换为您的 Web 目录的实际名称)(确保包含尾随 /)
无论如何,这是我的(衍生)代码:
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:
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:
嘿多米尼克,你的回答很好,但是如果你有一个像 http://localhost/project/index.php “project”链接会重复,因为它是 $base 的一部分,并且也出现在 $path 数组中。所以我调整并删除了 $path 数组中的第一项。
我不知道这是否是最优雅的方式,但它现在对我有用。
我在第 13 行或其他地方添加了该代码之前
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.
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
这是一个很棒的简单动态面包屑(根据需要进行调整):
Here is a great simple dynamic breadcrumb (tweak as needed):
使用
explode()
函数的更好方法如下...不要忘记替换超链接
href
中的 URL 变量。A better one using
explode()
function is as follows...Don't forget to replace your URL variable in the hyperlink
href
.这是我基于怀疑论答案的解决方案。它从 WordPress DB 获取页面标题,而不是从 URL 获取,因为存在拉丁字符问题(slug 没有拉丁字符)。您还可以选择是否显示“主页”项目。
使用:
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.
Using:
这是我个人在我的网站中使用的代码。在盒子之外工作。
CSS:
随意调整 CSS 内边距和边距,直到适合您自己的网站为止。
This is the code that i personally use in my sites. Works outside of the box.
The css:
Feel free to play around with the css padding and margins until you get it right for your own site.
}
}
我还有其他要求,需要一个更灵活的解决方案,即我的网络服务器上“某处”文件夹的面包屑路径,该文件夹不在我网站的文件结构内。因此,我无法在这里使用大多数解决方案。
在我的 Windows 计算机上,路径如下所示:
C:\wamp64\www\VIBE\screening_service\public\患者\病人_00005238\screening 20210608_1827
我希望我的面包屑看起来像这样:
public >患者>病人_00005238>筛查20210608_1827
因此,“public”是开始文件夹(我在代码中将其称为$baseFolder)。
对于拇指的显示文本,我可以获取属于这些文件夹的 URL,但我必须在代码中保留一部分(我将其称为 $baseUrl)。我需要省略的部分:
/VIBE/screening_service/
通过稍后合并 $baseUrl '/VIBE/screening_service/”与“患者/病人_00005238”,您通常可以创建有效的链接。但在我的情况下则不然,因为这些文件夹位于我的网站之外
Web 浏览器会将合并的字符串
'/VIBE/screening_service/ Patients/ Patient_00005238/'
解释为
'< em>http://localhost:8080//VIBE/screening_service/患者/patent_00005238/'。
在我的例子中,我只想要面包屑路径(如“患者/病人_00005238”),我将其添加到面包屑 div 的数据属性中。
我定义了一个 Javascript 单击事件,并将使用数据属性来重建我需要的真实路径。
好吧,这一切导致了这个功能,这可能对某人有用。
像这样调用请
注意,$baseUrl 映射到 Web 服务器上的 $baseFolder。
I had other requirements and needed a far more flexible solution, a bread crumbs path for a folder 'somewhere' on my web server, which is not within the file structure of my website. Because of this, I can't use most solutions here.
On my Windows machine the path looks like this:
C:\wamp64\www\VIBE\screening_service\public\patients\patient_00005238\screening 20210608_1827
I wanted my breadcrumbs look like this:
public › patients › patient_00005238 › screening 20210608_1827
So, 'public' is the start folder (which I call $baseFolder in my code).
For the displayed text of the thumbs, I can take the URL's which belong to these folders, but I have to leave a part out, (which I will call $baseUrl) in my code. The part I need to leave out:
/VIBE/screening_service/
By later merging $baseUrl '/VIBE/screening_service/' with 'patients/patient_00005238', you can normally create valid links. Not in my case though, because these folders are outside of my website
A web browser will interpret the merged strings
'/VIBE/screening_service/patients/patient_00005238/'
as
'http://localhost:8080//VIBE/screening_service/patients/patient_00005238/'.
In my case I wanted just the crumb path ( like 'patients/patient_00005238' ), which I add to the data attribute of the div of the crumb.
I define a Javascript click event and will use the data attribute to reconstruct the real path I need.
Well all this resulted in this function, which might be useful to somebody.
Call it like this
Note that $baseUrl maps to $baseFolder on the web server.