ThinkPhp获取栏目和文章当前位置的方法代码

分类:PHP_Python| 发布:kuabaobao| 查看: | 发表时间:2015/6/4

今天把博客一些细节完善了一下,其中修改了一下栏目页和文章页中的“当前位置”。以前栏目很少,就用死办法做的(首页 -> 栏目的名字),现在栏目多了,渐渐二级栏目,三级栏目也来了,这样的方式显然不太合适,于是就改进了一下。也不难,利用一个递归函数就可以了。

测试效果如下图所示:

查看源文件效果:

代码如下:
<a href="http://camnpr.com">首页</a> -> <a href="/category/php-python.html">PHP学习</a> -> <a href="/cat_9.html">ecshop</a> -> <a href="/cat_13.html">ecshop二次开发</a> -> ecshop加入百度地图,支持周边标记
代码如下:
//当前位置-第一个参数 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://camnpr.com">首页</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;
}
365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/php-python/2024.html