1

Zblog实现分类模板添加自定义排序方法

 2 years ago
source link: https://www.huhexian.com/22996.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Zblog实现分类模板添加自定义排序方法

青山 2022-01-1313:21:52评论

话说首款资源类主题模板终于要上线了,目前已经完成了基本功能设置,正在做适应的兼容,资源类主题模板涉及的接口较多,而且目前上线的主题中基本都有一个常用的功能,那就是分类模板页的排序,可以自由组合,按照时间、浏览、评论等等,部分主题模板还可能设计了免费、收费、VIP等等功能,所以这个排序是必要的。参考了百度和几位朋友的介绍算是实现了,但是跟可风大佬聊天的时候顺带说了这个事,因为最初的排序有一部分代码是可风写的,然后大佬告诉我有更简介高效的代码,好吧,有技术就是任性,不多说,直接上代码。

首先挂载接口,在主题核心文件(include.php)中挂载接口

  1. Add_Filter_Plugin('Filter_Plugin_LargeData_Article', 'LargeData_Article');

然后接口对应代码:

  1. //分类模板排序function LargeData_Article($select, $w, &$order, $limit, $option, $type){
  2. global $zbp;
  3. switch($type){
  4. case 'category':
  5. //分类模板标签,此段可删除
  6. case 'tag':
  7. //标签模板标签,此段可删除
  8. $pagebar = $option['pagebar'];
  9. $sort = GetVars('sort','GET') ? 'ASC' : 'DESC';
  10. switch($o = GetVars('order','GET')){
  11. case 'view':
  12. $order = array('log_ViewNums' => $sort);
  13. break;
  14. case 'comment':
  15. $order = array('log_CommNums' => $sort);
  16. break;
  17. case 'newest':
  18. default:
  19. $order = array('log_PostTime' => $sort);
  20. $sort == 'DESC' && $o = null;
  21. break;
  22. if ($o){
  23. $pagebar->UrlRule->__construct($zbp->option['ZC_CATEGORY_REGEX'] .($zbp->Config('system')->ZC_STATIC_MODE != 'REWRITE' ? '&' : '?'). 'order={%order%}&sort={%sort%}');
  24. $pagebar->UrlRule->Rules['{%order%}'] = $o;
  25. $pagebar->UrlRule->Rules['{%sort%}'] = (int)GetVars('sort','GET');
  26. break;

其次在模板添加代码:找到主题分类模板(catalog.php)文件,在适当的位置加入排序代码:

  1. <form id="sort-list" class="filter-tag">
  2. <span class="filter-l"><i class="icon font-paixu"></i>排序</span>
  3. <li class="filter order">
  4. <a href="" rel="nofollow" class="{if GetVars('order','GET') == 'newest' || !GetVars('order','GET')}current{/if}" data-type="newest">最新<i class="icon font-chevron-{if GetVars('sort','GET')}up{else}down{/if}"></i></a>
  5. <a href="" rel="nofollow" class="{if GetVars('order','GET') == 'view'}current{/if}" data-type="view">浏览<i class="icon font-chevron-{if GetVars('sort','GET')}up{else}down{/if}"></i></a>
  6. <a href="" rel="nofollow" class="{if GetVars('order','GET') == 'comment'}current{/if}" data-type="comment">评论<i class="icon font-chevron-{if GetVars('sort','GET')}up{else}down{/if}"></i></a>
  7. </li>
  8. {if $zbp->Config('system')->ZC_STATIC_MODE != 'REWRITE'}<input type="hidden" name="cate" value="{$category->ID}">{/if}
  9. <input type="hidden" name="order" value="{GetVars('order','GET')}">
  10. <input type="hidden" name="sort" value="{php}echo (int)GetVars('sort','GET'){/php}"></form>

添加之后css样式自己修改,图标采用的是阿里图标库,如果您的奥森图标或者其他请自行更改<i class="icon font-paixu"></i>代码,值得注意的是 name="cate" 默认是指分类模板,如果是标签模板页需要修改成 name="tags" 就可以了。

最后添加js代码:找到主题模板自带的js文件,在最后的位置添加如下代码,如果是放在html里,记得首位加上<script>如下代码</script>

  1. //分类排序!function(f){
  2. var a=f.find('.order a'),o=f.find('[name=order]'),s=f.find('[name=sort]');
  3. a.click(function(){
  4. var v=$(this).data('type');
  5. if(v===o.val()){
  6. s.val(s.val().toString()==='1'?0:1);
  7. }else{
  8. s.val(''===o.val() && !$(this).index() ? 1 : 0);
  9. o.val(v);
  10. f.submit();
  11. return false;
  12. })}($('#sort-list'))

效果预览:

Zblog实现分类模板添加自定义排序方法

大功告成,勇于尝试用问题留言反馈,反正我也不知道怎么解决。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK