dedeCMS中单独调用子栏目模板和子栏目的文章时修改源代码给channel和chanenartllist加上limit
在网站文件中找到include-taglib-chanel.lib.php,和chaneartllist.lib.php
下载用php的IDE打开,
chanel.php加入limit属性修改如下
<?php /** * 获取栏目列表标签 * * @version $Id: channel.lib.php 1 9:29 2010年7月6日Z tianya $ * @package DedeCMS.Taglib * @copyright Copyright (c) 2007 - 2010, DesDev, Inc. * @license http://help.dedecms.com/usersguide/license.html * @link http://www.dedecms.com */ /*>>dede>> <name>频道标签</name> <type>全局标记</type> <for>V55,V56,V57</for> <description>用于获取栏目列表</description> <demo> {dede:channel type='top' row='8' currentstyle="<li><a href='~typelink~' class='thisclass'>~typename~</a> </li>"} <li><a href='[field:typelink/]'>[field:typename/]</a> </li> {/dede:channel} </demo> <attributes> <iterm>typeid:栏目ID</iterm> <iterm>reid:上级栏目ID</iterm> <iterm>row:调用栏目数</iterm> <iterm>col:分多少列显示(默认为单列)</iterm> <iterm>type:son表示下级栏目,self表示同级栏目,top顶级栏目</iterm> <iterm>currentstyle:应用样式</iterm> </attributes> >>dede>>*/ function lib_channel(&$ctag,&$refObj) { global $dsql; $attlist = "typeid|0,reid|0,row|100,col|1,type|son,currentstyle|,cacheid|,limit|"; FillAttsDefault($ctag->CAttribute->Items,$attlist); extract($ctag->CAttribute->Items, EXTR_SKIP); $innertext = $ctag->GetInnerText(); $line = empty($row) ? 100 : $row; $limit=empty($limit)?0:$limit; $likeType = ''; //读取固定的缓存块 $cacheid = trim($cacheid); if($cacheid !='') { $likeType = GetCacheBlock($cacheid); if($likeType != '') return $likeType; } $reid = 0; $topid = 0; //如果属性里没指定栏目id,从引用类里获取栏目信息 if(empty($typeid)) { if( isset($refObj->TypeLink->TypeInfos['id']) ) { $typeid = $refObj->TypeLink->TypeInfos['id']; $reid = $refObj->TypeLink->TypeInfos['reid']; $topid = $refObj->TypeLink->TypeInfos['topid']; } else { $typeid = 0; } } //如果指定了栏目id,从数据库获取栏目信息 else { $row2 = $dsql->GetOne("SELECT * FROM `#@__arctype` WHERE id='$typeid' "); $typeid = $row2['id']; $reid = $row2['reid']; $topid = $row2['topid']; $issetInfos = true; } if($type=='' || $type=='sun') $type='son'; if($innertext=='') $innertext = GetSysTemplets("channel_list.htm"); if($type=='top') { $sql = "SELECT id,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl,sitepath From `#@__arctype` WHERE reid=0 And ishidden<>1 order by sortrank asc limit $limit,$line "; } else if($type=='son') { if($typeid==0) return ''; $sql = "SELECT id,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl,sitepath From `#@__arctype` WHERE reid='$typeid' And ishidden<>1 order by sortrank asc limit $limit,$line "; } else if($type=='self') { if($reid==0) return ''; $sql = "SELECT id,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl,sitepath FROM `#@__arctype` WHERE reid='$reid' And ishidden<>1 order by sortrank asc limit $limit,$line "; } //And id<>'$typeid' $needRel = false; $dtp2 = new DedeTagParse(); $dtp2->SetNameSpace('field','[',']'); $dtp2->LoadSource($innertext); //检查是否有子栏目,并返回rel提示(用于二级菜单) if(preg_match('#:rel#', $innertext)) $needRel = true; if(empty($sql)) return ''; $dsql->SetQuery($sql); $dsql->Execute(); $totalRow = $dsql->GetTotalRow(); //如果用子栏目模式,当没有子栏目时显示同级栏目 if($type=='son' && $reid!=0 && $totalRow==0) { $sql = "SELECT id,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl,sitepath FROM `#@__arctype` WHERE reid='$reid' And ishidden<>1 order by sortrank asc limit 0, $line "; $dsql->SetQuery($sql); $dsql->Execute(); } $GLOBALS['autoindex'] = 0; for($i=0;$i < $line;$i++) { if($col>1) $likeType .= "<dl>\r\n"; for($j=0; $j<$col; $j++) { if($col>1) $likeType .= "<dd>\r\n"; if($row=$dsql->GetArray()) { $row['sonids'] = $row['rel'] = ''; if($needRel) { $row['sonids'] = GetSonIds($row['id'], 0, false); if($row['sonids']=='') $row['rel'] = ''; else $row['rel'] = " rel='dropmenu{$row['id']}'"; } //处理同级栏目中,当前栏目的样式 if( ($row['id']==$typeid || ($topid==$row['id'] && $type=='top') ) && $currentstyle!='' ) { $linkOkstr = $currentstyle; $row['typelink'] = GetOneTypeUrlA($row); $linkOkstr = str_replace("~rel~",$row['rel'],$linkOkstr); $linkOkstr = str_replace("~id~",$row['id'],$linkOkstr); $linkOkstr = str_replace("~typelink~",$row['typelink'],$linkOkstr); $linkOkstr = str_replace("~typename~",$row['typename'],$linkOkstr); $likeType .= $linkOkstr; } else { $row['typelink'] = $row['typeurl'] = GetOneTypeUrlA($row); if(is_array($dtp2->CTags)) { foreach($dtp2->CTags as $tagid=>$ctag) { if(isset($row[$ctag->GetName()])) $dtp2->Assign($tagid,$row[$ctag->GetName()]); } } $likeType .= $dtp2->GetResult(); } } if($col>1) $likeType .= "</dd>\r\n"; $GLOBALS['autoindex']++; } //Loop Col if($col>1) { $i += $col - 1; $likeType .= " </dl>\r\n"; } } //Loop for $i $dsql->FreeResult(); if($cacheid !='') { WriteCacheBlock($cacheid, $likeType); } return $likeType; }
这时候就可以在模板中调用了
比如
{dede:channel limit="0" row="1"} <dt><h3><a target="_blank" href='[field:typelink/]'>[field:typename/]</a></h3></dt> {/dede:channel}
这段代码表示调用从0开始,row为1行的子栏目,如下一个就将limit=“0”改为limit=“1”即可。
下面是chanelartlist中添加limit属性的源代码
<?php if(!defined('DEDEINC')) exit('Request Error!'); /** * 获取当前频道的下级栏目的内容列表标签 * * @version $Id: channelartlist.lib.php 1 9:29 2010年7月6日Z tianya $ * @package DedeCMS.Taglib * @copyright Copyright (c) 2007 - 2010, DesDev, Inc. * @license http://help.dedecms.com/usersguide/license.html * @link http://www.dedecms.com */ /*>>dede>> <name>频道文档</name> <type>全局标记</type> <for>V55,V56,V57</for> <description>获取当前频道的下级栏目的内容列表标签</description> <demo> {dede:channelartlist row=6} <dl> <dt><a href='{dede:field name='typeurl'/}'>{dede:field name='typename'/}</a></dt> <dd> {dede:arclist titlelen='42' row='10'} <ul class='autod'> <li><a href="[field:arcurl /]">[field:title /]</a></li> <li>([field:pubdate function="MyDate('m-d',@me)"/])</li> </ul> {/dede:arclist} </dl> {/dede:channelartlist} </demo> <attributes> <iterm>typeid:频道ID</iterm> <iterm>row:获取的栏目返回值</iterm> </attributes> >>dede>>*/ require_once(DEDEINC.'/arc.partview.class.php'); function lib_channelartlist(&$ctag,&$refObj) { global $dsql,$envs,$_sys_globals; //处理标记属性、innertext $attlist = 'typeid|0,row|20,cacheid|,limit|,type|'; //这边加个limit变量 type 我是想实现 在子栏目下调用 同级子栏目的信息 FillAttsDefault($ctag->CAttribute->Items,$attlist); extract($ctag->CAttribute->Items, EXTR_SKIP); $innertext = trim($ctag->GetInnerText()); $artlist = ''; //读取固定的缓存块 $cacheid = trim($cacheid); if($cacheid !='') { $artlist = GetCacheBlock($cacheid); if($artlist!='') return $artlist; } if(empty($typeid)) { $typeid = ( !empty($refObj->TypeLink->TypeInfos['id']) ? $refObj->TypeLink->TypeInfos['id'] : 0 ); } if($innertext=='') $innertext = GetSysTemplets('part_channelartlist.htm'); $totalnum = $row; if(empty($totalnum)) $totalnum = 20; //获得类别ID总数的信息 $typeids = array(); if($type=='reid') //2011.7.5 调用上级目录下的子栏目内容 { $reid = $refObj->TypeLink->TypeInfos['reid']; $tpsql = " reid='$reid' AND ispart<>2 AND ishidden<>1 "; } else if($typeid==0 || $typeid=='top') { $tpsql = " reid=0 AND ispart<>2 AND ishidden<>1 AND channeltype>0 "; } else { if(!preg_match('#,#', $typeid)) { $tpsql = " reid='$typeid' AND ispart<>2 AND ishidden<>1 "; } else { $tpsql = " id IN($typeid) AND ispart<>2 AND ishidden<>1 "; } } $limit = trim(preg_replace('#limit#is', '', $limit)); //增加limit标签 if($limit!='') $limitsql = " LIMIT $limit "; //如果limit为空就以 row变量的默认值 限制 else $limitsql = " LIMIT 0,$totalnum"; $dsql->SetQuery("SELECT id,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl,sitepath FROM `dede_arctype` WHERE $tpsql ORDER BY sortrank ASC $limitsql"); $dsql->Execute(); while($row = $dsql->GetArray()) { $typeids[] = $row; } if(!isset($typeids[0])) return ''; $GLOBALS['itemindex'] = 0; $GLOBALS['itemparity'] = 1; for($i=0;isset($typeids[$i]);$i++) { $GLOBALS['itemindex']++; $pv = new PartView($typeids[$i]['id']); $pv->Fields['typeurl'] = GetOneTypeUrlA($typeids[$i]); $pv->SetTemplet($innertext,'string'); $artlist .= $pv->GetResult(); $GLOBALS['itemparity'] = ($GLOBALS['itemparity']==1 ? 2 : 1); } //注销环境变量,以防止后续调用中被使用 $GLOBALS['envs']['typeid'] = $_sys_globals['typeid']; $GLOBALS['envs']['reid'] = ''; if($cacheid !='') { WriteCacheBlock($cacheid, $artlist); } return $artlist; }
使用方法如下:
{dede:channelartlist limit='0,1'} {dede:arclist row='6' titlelen='42'} <dd><a target="_blank" href="[field:arcurl/]" style="">[field:title/]</a></dd> {/dede:arclist} {/dede:channelartlist}
从栏目的第一个子栏目读取6篇文章
如需读取第二个子栏目的文章,limit=“1,1”,第三为:limit=“2,1”
依次类推。
经验分享!不谢!