Drupal 6 自定义分页样式一例

在主题下的template.php文件中,增加下面的函数:

function default_pager($tags = array(), $limit = 11, $element = 0, $parameters = array(), $quantity = 4) {
  global $pager_page_array, $pager_total;
  $output = '';  //保存分页的html字符串

  //获取当前URL,如果其中已经有page=x,则删除它
  $this_path=request_uri();
  $temp_pager=$_GET['page'];
  if(is_numeric($temp_pager)){
    $this_path=str_replace('&page='.$temp_pager,'',$this_path);
  }

  if ($pager_total[$element] > 1) { //如果只有一页,则无分页
    $pager_cur=$pager_page_array[$element];  //当前页面的页码

    if($_GET['q']=='news'){ //如果当前路径为 news,则采用上下页的分页方式。
      if($pager_cur>0){
        $output .= '<input name="" class="btn" type="button" value="上一页"  javascript:  onclick="javascript:window.location.href='."'".$this_path."&page=".($pager_page_array[$element]-1)."'".'" />';
      }
      if(($pager_cur+1)<$pager_total[$element]){
        $output .= '<input name="" class="btn" type="button" value="下一页"  javascript:  onclick="javascript:window.location.href='."'".$this_path."&page=".($pager_page_array[$element]+1)."'".'" />';
      }

      return $output;
    }

    $output = '<div class="content_pager">'."\n".'<div class="com_pager">';
    $pager_list=array();  //current available pager numbers
    $n=floor($pager_cur/$quantity);

    if ($n == 0){
      $i=0;
      $max=$quantity;
    }
    else {
      $i=$quantity*$n;
      $max=$i + $quantity;
    }

    while($i<($pager_total[$element]) && $i<$max){
      $pager_list[$i]=array('pager'=>$i,'title'=>$i+1);
      $i+=1;
    }

    foreach($pager_list as $key=>$item){

      if($pager_cur < $quantity) {
        $output.='<span class="disabled"> < </span>';
      }
      else {
        $output.='<a href="'.$this_path.'&page='.($item['pager']-1).'"> < </a>';
      }

      if($item['pager'] == $pager_cur) {
        $output.='<span class="current">'.$item['title'].'</span>';
      }
      else {
        $output.='<a href="'.$this_path.'&page='.$item['pager'].'"> '.$item['title'].' </a>';
      }
    }

    if ($item['pager'] < ($pager_total[$element]-1)) {
      $output.='<a href="'.$this_path.'&page='.($item['pager']+1).'"> > </a>';
    }
    else {
      $output.='<span class="disabled"> > </span>';
    }

    $output.= '</div>'."\n".'</div>';
  }

  return $output;
}

在需要分页的地方打印$pager即可(<?php print $pager; ?>),但前提条件是查询时使用了分页查询:pager_query($sql, $step, 0, $countsql);

该函数在Drupal 6下测试通过。

发表评论

邮箱地址不会被公开。 必填项已用*标注

机器人检查 *

分享我的最新文章标题到这里

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据