前端很多时候需要直接获取一个栏目的相关信息,获取在某个页面上获取顶级或者上一级栏目的相关信息,这样我们可以自己写个自定义方法来实现。
打开 /include/extend.func.php 在最下面添加
/*** 获取顶级栏目相关信息** @access public* @param string $tid 栏目id* @param string $field 栏目字段* @return string*/if ( ! function_exists('getToptype')){function getToptype($tid,$field){global $dsql,$cfg_Cs;if(!is_array($cfg_Cs)){require_once(DEDEDATA."/cache/inc_catalog_base.inc");}if(!isset($cfg_Cs[$tid][0]) || $cfg_Cs[$tid][0]==0){$topid = $tid;}else{$topid = GetTopid($cfg_Cs[$tid][0]);}$row = $dsql->GetOne("SELECT * FROM `dede_arctype` WHERE id=$topid");if($field=='id') return $topid;if($field=='typeurl' || $field=='typelink') return GetOneTypeUrlA($row);return $row[$field];}}/*** 获取当前栏目相关信息** @access public* @param string $tid 栏目id* @param string $field 栏目字段* @return string*/if ( ! function_exists('getCurtype')){function getCurtype($tid,$field){global $dsql;$row = $dsql->GetOne("SELECT * FROM `dede_arctype` WHERE id=$tid");if($field=='id') return $tid;if($field=='typeurl' || $field=='typelink') return GetOneTypeUrlA($row);return $row[$field];}}/*** 获取上级栏目相关信息** @access public* @param string $tid 栏目id* @param string $field 栏目字段* @return string*/if ( ! function_exists('getRetype')){function getRetype($tid,$field){global $dsql;$typeid = $tid;$query = "SELECT reid FROM `dede_arctype` where id = $typeid";$rs = $dsql->GetOne($query);$reid = $rs['reid'];$query2 = "SELECT * FROM `dede_arctype` where id = $reid";$row = $dsql->GetOne($query2);if($field=='id') return $row['id'];if($field=='typeurl' || $field=='typelink') return GetOneTypeUrlA($row);return $row[$field];}}
模板里调用标签写法
需要哪个字段,就对应的填上字段名即可
获取顶级栏目相关信息
{dede:field.typeid function=getToptype(@me,typename)/}
获取当前栏目相关信息
{dede:field.typeid function=getCurtype(@me,typename)/}
获取上级栏目相关信息
{dede:field.typeid function=getRetype(@me,typename)/}
不喜欢添加自定义方法的同学可以试试这样
{dede:field.typeid runphp=yes}global $dsql;$row = $dsql->GetOne("SELECT * FROM dede_arctype WHERE id=@me");$reid = "$row[reid]"; $topid = "$row[topid]";if($reid == 0) @me = $row[content];else $row = $dsql->GetOne("SELECT * FROM dede_arctype WHERE id=$topid");@me = $row[content];{/dede:field.typeid}
获取顶级的是topid
获取上级的是reid
案例
栏目列表页加入【全部】按钮链接,并且高亮
<ul><li><a href="{dede:field.typeid runphp=yes}global $dsql;$row = $dsql->GetOne("SELECT * FROM dede_arctype WHERE id=@me");$reid = "$row[reid]";$topid = "$row[topid]";if($reid == 0) @me = GetOneTypeUrlA($row);else $row = $dsql->GetOne("SELECT * FROM dede_arctype WHERE id=$topid");@me = GetOneTypeUrlA($row);{/dede:field.typeid}"{dede:field name=topid runphp=yes}if(@me)@me=''; else @me=' class="on"';{/dede:field}>全部</a></li>{dede:channel type='son' row='10' currentstyle="<li><a href='~typelink~' class='on'>~typename~</a></li>"}<li><a href='[field:typeurl/]' [field:rel/]>[field:typename/]</a></li>{/dede:channel}</ul>