无为清净楼资源网 Design By www.qnjia.com

本文实例讲述了thinkphp获取栏目和文章当前位置的方法。分享给大家供大家参考。具体实现方法如下:

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

测试效果如下图所示:

thinkphp获取栏目和文章当前位置的方法

查看源文件效果:复制代码 代码如下:<a href="https://www.jb51.net">首页</a> -> <a href="/cat_2.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="https://www.jb51.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;
}

希望本文所述对大家的PHP程序设计有所帮助。

标签:
thinkphp,获取,栏目,文章,当前位置

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com