织梦DedeCMS5.7获取顶级栏目名称的标签
下面是调用顶级栏目名称的方法:
方法一:
{dede:field.typeid runphp=’yes’}$ID = @me; $sql = "SELECT * FROM `dede_arctype` WHERE id=$ID";$query = mysql_query($sql);$row=mysql_fetch_array($query); $relID = "$row[reid]"; if($relID == 0) {@me = $row[typename];}else{$sql = "SELECT `typename` FROM `dede_arctype` WHERE id=$relID";$query = mysql_query($sql);$row=mysql_fetch_array($query); @me = $row[typename];}{/dede:field.typeid}
这个方法语句有些长。
方法二:
{dede:field.title runphp='yes'}list($toptype,$sontype)=split('/',@me);@me=$toptype;{/dede:field.title}
方法三:
{dede:field.typeid runphp='yes'}$ID = @me; $sql = "SELECT * FROM `dede_arctype` WHERE id=$ID";$query = mysql_query($sql);$row=mysql_fetch_array($query); $relID = "$row[reid]"; $topID="$row[topid]"; if($relID == 0) {@me = $row[typename];}else{$sql = "SELECT `typename` FROM `dede_arctype` WHERE id=$topID";$query = mysql_query($sql);$row=mysql_fetch_array($query); @me = $row[typename];}{/dede:field.typeid}
方法四(推荐):
打开include/helpers/extend.helper.php,添加一个函数:
/**
* 获取指定栏目id的顶级栏目的名称 * * @return string */if ( ! function_exists('getTopChannelName')){function getTopChannelName($id) { global $dsql; $typeinfo = $dsql->GetOne("SELECT * FROM `dede_arctype` WHERE id='{$id}'"); if($typeinfo['reid']==0) return $typeinfo['typename']; else return getTopChannelName($typeinfo['reid']);}}
在列表页这么使用:{dede:field name=’id’ function=’getTopChannelName(@me)’/}
在内容页这么使用:{dede:field name=’typeid’ function=’getTopChannelName(@me)’/}
附带获取顶级栏目的url:
打开include/helpers/extend.helper.php,添加一个函数:
/**
* 获取指定栏目id的顶级栏目的url
*
* @return string */if ( ! function_exists('getTopChannelUrl')){function getTopChannelUrl($id) { global $dsql; $typeinfo = $dsql->GetOne("SELECT * FROM `dede_arctype` WHERE id='{$id}'"); if($typeinfo['reid']==0) return GetOneTypeUrlA($typeinfo); else return getTopChannelUrl($typeinfo['reid']);}}
使用方法同上。