正文

zblog分類模板添加自定義排序教程

Zblog資源類主題模板涉及的接口較多,而且目前上線的主題中基本都有一個常用的功能,那就是分類模板頁的排序,可以自由組合,按照時間、瀏覽、評論等等,部分主題模板還可能設(shè)計了免費、收費、VIP等等功能,所以這個排序是必要的。參考了百度和幾位朋友的介紹算是實現(xiàn)了,但是跟可風(fēng)大佬聊天的時候順帶說了這個事,因為Zui初的排序有一部分代碼是可風(fēng)寫的,然后大佬告訴我有更簡介高效的代碼,好吧,有技術(shù)就是任性,不多說,直接上代碼。

zblog分類模板添加自定義排序教程(圖1)

首先掛載接口

需要在主題核心文件(include.php)中掛載接口

Add_Filter_Plugin('Filter_Plugin_LargeData_Article','LargeData_Article');

然后接口對應(yīng)代碼:

//分類模板排序
functionLargeData_Article($select,$w,&$order,$limit,$option,$type){
global$zbp;
switch($type){
case'category':
//分類模板標(biāo)簽,此段可刪除
case'tag':
//標(biāo)簽?zāi)0鍢?biāo)簽,此段可刪除
$pagebar=$option['pagebar'];
$sort=GetVars('sort','GET')?'ASC':'DESC';
switch($o=GetVars('order','GET')){
case'view':
$order=array('log_ViewNums'=>$sort);
break;
case'comment':
$order=array('log_CommNums'=>$sort);
break;
case'newest':
default:
$order=array('log_PostTime'=>$sort);
$sort=='DESC'&&$o=null;
break;
}
if($o){
$pagebar->UrlRule->__construct($zbp->option['ZC_CATEGORY_REGEX'].($zbp->Config('system')->ZC_STATIC_MODE!='REWRITE'?'&':'?').'order={%order%}&sort={%sort%}');
$pagebar->UrlRule->Rules['{%order%}']=$o;
$pagebar->UrlRule->Rules['{%sort%}']=(int)GetVars('sort','GET');
}
break;
}
}

其次在模板添加代碼:

找到主題分類模板(catalog.php)文件,在適當(dāng)?shù)奈恢眉尤肱判虼a:

<formid="sort-list"class="filter-tag">
<spanclass="filter-l"><iclass="iconfont-paixu"></i>排序</span>
<liclass="filterorder">
<ahref=""rel="nofollow"class="{ifGetVars('order','GET')=='newest'||!GetVars('order','GET')}current{/if}"data-type="newest">Zui新<iclass="iconfont-chevron-{ifGetVars('sort','GET')}up{else}down{/if}"></i></a>
<ahref=""rel="nofollow"class="{ifGetVars('order','GET')=='view'}current{/if}"data-type="view">瀏覽<iclass="iconfont-chevron-{ifGetVars('sort','GET')}up{else}down{/if}"></i></a>
<ahref=""rel="nofollow"class="{ifGetVars('order','GET')=='comment'}current{/if}"data-type="comment">評論<iclass="iconfont-chevron-{ifGetVars('sort','GET')}up{else}down{/if}"></i></a></li>
{if$zbp->Config('system')->ZC_STATIC_MODE!='REWRITE'}<inputtype="hidden"name="cate"value="{$category->ID}">{/if}
<inputtype="hidden"name="order"value="{GetVars('order','GET')}">
<inputtype="hidden"name="sort"value="{php}echo(int)GetVars('sort','GET'){/php}">
</form>

添加之后css樣式自己修改,圖標(biāo)采用的是阿里圖標(biāo)庫,如果您的奧森圖標(biāo)或者其他請自行更改<i class="icon font-paixu"></i>代碼,值得注意的是 name="cate" 默認(rèn)是指分類模板,如果是標(biāo)簽?zāi)0屙撔枰薷某?name="tags" 就可以了。

Zui后添加js代碼:

找到主題模板自帶的js文件,在Zui后的位置添加如下代碼,如果是放在html里,記得首位加上<script>如下代碼</script>

//分類排序
!function(f){
vara=f.find('.ordera'),o=f.find('[name=order]'),s=f.find('[name=sort]');
a.click(function(){
varv=$(this).data('type');
if(v===o.val()){
s.val(s.val().toString()==='1'?0:1);
}else{
s.val(''===o.val()&&!$(this).index()?1:0);
o.val(v);
}
f.submit();
returnfalse;
})
}($('#sort-list'))

效果如開篇的圖,大功告成。

來源:李洋博客

聲明:月季網(wǎng)致力月季產(chǎn)業(yè),部分文字和圖片來源網(wǎng)絡(luò),版權(quán)歸原作者,如有侵權(quán)請聯(lián)系刪除。

月季網(wǎng)

月季網(wǎng)