thinkphp实现面包屑导航(当前位置)例子分享

前端技术 2023/09/04 PHP

以前栏目很少,就用死办法做的(首页 -> 栏目的名字),现在栏目多了,渐渐二级栏目,三级栏目也来了,这样的方式显然不太合适,于是就改进了一下。也不难,利用一个递归函数就可以了。

使用例子:

复制代码 代码如下:

//当前位置-第一个参数 catid为当前栏目的id,第二个参数为文章的标题,调用栏目当前位置时第二个参数为空即可。
$this->assign(\"now_here\",$this->now_here($catid,$res[\'title\']));

实现代码:

复制代码 代码如下:

//解释一下,栏目表category中的catid为栏目id,catname为栏目名称,asmenu为栏目父级的id,当为顶级栏目时,asmenu为0 。
protected function now_here($catid,$ext=\'\'){
 $cat = M(\"Category\");
 $here = \'<a href=\"http://www.phpstudy.net\">首页</a>\';
 $uplevels = $cat->field(\"catid,catname,asmenu\")->where(\"catid=$catid\")->find();
 if($uplevels[\'asmenu\'] != 0)
 $here .= $this->get_up_levels($uplevels[\'asmenu\']);
 $here .= \' -> <a href=\"/cat_\'.$uplevels[\'catid\'].\'.html\">\'.$uplevels[\'catname\'].\"</a>\";
 if($ext != \'\') $here .= \' -> \'.$ext;
 return $here;
}
protected function get_up_levels($id){
 $cat = M(\"Category\");
 $here = \'\';
 $uplevels = $cat->field(\"catid,catname,asmenu\")->where(\"catid=$id\")->find();
 $here .= \' -> <a href=\"/cat_\'.$uplevels[\'catid\'].\'.html\">\'.$uplevels[\'catname\'].\"</a>\";
 if($uplevels[\'asmenu\'] != 0){
  $here = $this->get_up_levels($uplevels[\'asmenu\']).$here;
 }
 return $here;
}

本文地址:https://www.stayed.cn/item/11025

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。