DedeCMS自增函数autoindex/itemindex用法全解析
在使用DedeCMS建站的过程中,首页或者列表页的模版会涉及到调用栏目或者子栏目的问题,在调用栏目的时候会在第一个栏目链接中加特别的属性,这里就要用到DedeCMS自带的自增函数[field:global name=autoindex/]和[field:global name=itemindex/],余斗今天就来说说这个[field:global name=autoindex/]和[field:global name=itemindex/]自增函数的一些用法与扩展。
基本用法
autoindex与itemindex都是中都是用@me来表示计数开始,而类似@me+1则可以指定数字开始,在不同的标签中,他们的起始值是不同的:
channelartlist 标签下使用 {dede:global name=’itemindex’ runphp=’yes’}@me;{/dede:global} 是从0开始自增1
channelartlist 标签下使用 {dede:global.itemindex/} 默认从1开始
arclist 标签下使用 [field:global.autoindex/] 默认从1开始
channel 标签下使用 [field:global.autoindex/] 默认从0开始
而如果想要改变起始值则按照以下方式调用:
从0开始[field:global name=autoindex runphp=”yes”]@me=@me-1;[/field:global]
从1开始[field:global name=autoindex runphp=”yes”]@me=@me+1;[/field:global]
其中,@me=@me±1就能实现对应的起始值,这里自己根据需要修改即可。
扩展用法
起始值加5开始自增计数
[field:
global name=autoindex runphp=
“yes”]@me=@me+
5;[/field:
global]
如果被2整除则输出竖线否则为空
[field:
global name=autoindex runphp=
“yes”](@me%
2==
0)? @me=
“|”:@me=
“”;[/field:
global]
如果不等于8输出竖线否则为空,即为8的时候不打印竖线
[field:
global name=autoindex runphp=
“yes”](@me!=
8)? @me=
“|”:@me=
“”;[/field:
global]
列表每5行有带划线
[field:
global runphp=
‘yes’
name=autoindex]
$a=
“<li>”;
$c=
“<li class=’line’>”;
if ((@me %
5) ==
0) @me = $c;
else @me = $a;
[/field:
global]
在第5行和第10行加广告,其他行为空
[field:
global runphp=
‘yes’
name=autoindex]
$a=
“<div class=’box’>”;
$b=
“广告1”;
$c=
“</div>”;
$d=
“广告2”;
$e=
“”;
if (@me ==
5) @me = $a.$b.$c;
else if (@me ==
10) @me = $a.$d.$c;
else @me = $e;
[/field:
global]
第一行样式为class=”check”,其他行按行数类名字后面id=”life_channe1″的数字1实现自增
<li><a
href=
“[field:typeurl/] “ [field:
global name=
‘autoindex’runphp=
‘yes’]
if(@me==
0){@me=
‘class=”check”‘;}
else{@me=
‘id=”life_channe’.@me.
‘” goto=”life_channe’.@me.
‘”‘;}[/field:
global]>[field:typename/]</a></li>
Div中id名字后面的数字从0开始自增,判断如果是第一行则加style=”display:none;”属性,其他行为空
<div
id=
“HomeCon2_1_{dede:global name=itemindex runphp=’yes’}@me=@me-1;{/dede:global}”
class=
“HomeCon2_1_1” {dede:
global name=
‘itemindex’ runphp=
‘yes’}@me=(@me==
1)?
”:
‘style=”display:none;”‘;{/dede:
global} >
对autoindex/itemindex使用自定义函数
先在include/extend.fun.php里添加自定义函数
function MyPosition($p){
$positionArr=array(
275,
330,
380,
435,
495,
547);
return $positionArr[$p];
模版中调用方法为:
{dede:channel type=
‘son’ typeid=
’13’ row=
‘6’ noself=
‘yes’}
<!
—–侧栏菜单——————>
<
div id=
‘pdv_16795’ class=
‘pdv_class’ title=
” style=
“width:71px;height:20px;top:[field:global.autoindex function=’MyPosition(@me)’/]px;left:136px; z-index:17”>
<
div style=
“FONT-FAMILY: SimSun; COLOR: #fecd2e; FONT-SIZE: 15px; fon-weight: bold”><a style=
“FONT-FAMILY: SimSun; COLOR: #fecd2e; FONT-SIZE: 15px; fon-weight: bold” href=
“[field:typeurl/]” target=_blank><strong>[field:typename/]</strong></a></
div>
</
div>
{/dede:channel}
织梦默认的搜索页不支持autoindex标签,需要修改核心文件增加支持
找到文件:include/arc.searchview.class.php
里面找到代码(大概在709行):
$this->dtp2->LoadSource($innertext);
修改为:
$this->dtp2->LoadSource($innertext);
$GLOBALS[
‘autoindex’] =
0;
找到代码(大概在722行):
if($row = $this->dsql->GetArray(
“al”))
{
修改为
if($row = $this->dsql->GetArray(
“al”))
{
$GLOBALS[
‘autoindex’]++;
$ids[$row[
‘id’]] = $row[
‘id’];
以上就是余斗整理的autoindex/itemindex’用法与扩展,希望能帮到大家。