微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

测试效果

查看源文件效果:<a href="http://www.daixiaorui.com">首页</a> -&gt; <a href="/cat_2.html">PHP学习</a> -&gt; <a href="/cat_9.html">ecshop</a> -&gt; <a href="/cat_13.html">ecshop二次开发</a> -&gt; 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://www.daixiaorui.com">首页</a>';

$uplevels = $cat->field("catid,catname,asmenu")->where("catid=$catid")->find();

if($uplevels['asmenu'] != 0)

$here .= $this->get_up_levels($uplevels['asmenu']);

$here .= ' -&gt; <a href="/cat_'.$uplevels['catid'].'.html">'.$uplevels['catname']."</a>";

if($ext != '') $here .= ' -&gt; '.$ext;

return $here;

}

protected function get_up_levels($id){

$cat = M("Category");

$here = '';

$uplevels = $cat->field("catid,asmenu")->where("catid=$id")->find();

$here .= ' -&gt; <a href="/cat_'.$uplevels['catid'].'.html">'.$uplevels['catname']."</a>";

if($uplevels['asmenu'] != 0){

$here = $this->get_up_levels($uplevels['asmenu']).$here;

}

return $here;

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐