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行):

DedeCMS自增函數autoindex/itemindex用法全解析


$this->dtp2->LoadSource($innertext);
 

修改爲:


$this->dtp2->LoadSource($innertext);
$GLOBALS['autoindex'] = 0;
 

找到代碼(大概在722行):

DedeCMS自增函數autoindex/itemindex用法全解析


if($row = $this->dsql->GetArray("al"))
                                {
 

修改爲


  if($row = $this->dsql->GetArray("al"))
                                {
                                        $GLOBALS['autoindex']++;
                                        $ids[$row['id']] = $row['id'];
 

發佈了114 篇原創文章 · 獲贊 31 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章