织梦dedecms调用当前文章的顶级栏目名称的方法(包含顶级栏目任意字段)

织梦dedecms在模板调用中非常灵活,但是有些功能却无法实现,或者说实现起来难度比较大。最近在做一个织梦站点,由于所做的站点灵活性高,所以很多东西需要摸索。比如说织梦的列表页面和文章页面调用顶级栏目的栏目名称,试了好多种办法都无法实现,只能调用当前栏目的栏目名称,如果文章在二级栏目下面就无法调用顶级栏目。

{dede:type}[field:typename /]{/dede:type},这个只能显示当前栏目的名称和url,最后换了一种思路,索性重新写个type.lib.php文件,就命名为:type2.lib.php, 模板中使用这个来调用{dede:type2}[field:typename /]{/dede:type2} ,也可以调用顶级栏目的任意字段。
\include\taglib\目录下新建type2.lib.php代码如下:

<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +———————————————————————-+
// | PHP version 5 |
// +———————————————————————-+
// | Copyright (c) 1997-2004 The PHP Group |
// +———————————————————————-+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +———————————————————————-+
// | Authors: Original Author author@example.com |
// | Your Name you@example.com |
// +———————————————————————-+
//
// $Id:$

if (!defined(‘DEDEINC’)) exit(‘Request Error!’);
function lib_type2(&$ctag, &$refObj) {
global $dsql, $envs;
$attlist = ‘typeid|0’;
FillAttsDefault($ctag->CAttribute->Items, $attlist);
extract($ctag->CAttribute->Items, EXTR_SKIP);
$innertext = trim($ctag->GetInnerText());
if ($typeid == 0) {
$typeid = (isset($refObj->TypeLink->TypeInfos[‘topid’]) ? $refObj->TypeLink->TypeInfos[‘topid’] : $envs[‘typeid’]);
//echo “ssssssssss1”.$refObj->TypeLink->TypeInfos[‘topid’];
//echo “ssssssssss2”.$envs[‘typeid’]; exit;
}
//echo “typeid:”.$typeid; exit;
//lyy 如果topid==0 就是顶级分类,取当前记录
if($refObj->TypeLink->TypeInfos[‘topid’]==0){
$typeid=$refObj->TypeLink->TypeInfos[‘id’];
}
//echo “typeid:”.$typeid; exit;
if (empty($typeid)) return ”;
$row = $dsql->GetOne(“SELECT id,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl,sitepath
FROM

1
#@__arctype
WHERE id=’$typeid'”);

if (!is_array($row)) return ”;
if (trim($innertext) == ”) $innertext = GetSysTemplets(“part_type_list.htm”);
$dtp = new DedeTagParse();
$dtp->SetNameSpace(‘field’, ‘[‘, ‘]’);
$dtp->LoadSource($innertext);
if (!is_array($dtp->CTags)) {
unset($dtp);
return ”;
} else {
//lyy 以下getTypeUrl($row[topid]错的,换成 $row[‘id’] echo $row[‘id’].”sssss”;exit;

$row[‘typelink’] = GetTypeUrl($row[‘id’], MfTypedir($row[‘typedir’]) , $row[‘isdefault’], $row[‘defaultname’], $row[‘ispart’], $row[‘namerule2’], $row[‘siteurl’], $row[‘sitepath’]);

//print_r($row);exit;
foreach ($dtp->CTags as $tagid => $ctag) {
if (isset($row[$ctag->GetName() ])) $dtp->Assign($tagid, $row[$ctag->GetName() ]);
}
$revalue = $dtp->GetResult();
unset($dtp);
return $revalue;
}
} ?>

大家可以试一下,如果要调用自定义字段,需要在此添加字段名称, SELECT id,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl,sitepath FROM。