admin管理员组

文章数量:1290976

Hi fellow Code Poets )

I wanted the pagination links numbers to be in the order opposite to the pages they lead to. The link leading to the last page had to read 1, and vice versa, etc. I've written the code, and want to see if it can be improved.

Is there a way to improve it / write in a more wordpress-y way? My code looks a bit hacky to me.

The setup:

  • comments are divided into pages
  • older comments are below
  • there are 3 pages worth of comments

This gives me the newest comments on page 3, which is the first page displayed. Then goes page 2, and the oldest comments are on page 1

Accordingly, pagination link 1 is the oldest one, link3 is the newest one.

This confuses users.

I want to reverse JUST the links text numbers. So that link reading 1 would lead to page 3, etc.

My code:

$c_page = intval(get_query_var('cpage'));
$c_pages = get_comment_pages_count();

//bonus: a function to show the 'page N of X' line with the same reversed order of labels
if(!empty($c_page) && !empty($c_pages)){
   echo '<p class="cmpged">Page '.($c_pages - ($c_page-1)).' of '.$c_pages.'</p>';
}

$pagination = paginate_comments_links( array(
                        'prev_text' => '&rsaquo;',
                        'next_text' => '&lsaquo;',
                        'type' => 'array'
                    ));
    
$btns = array();
$links = array();

//split links into opening tag, content and closing tag
//set aside the 'previuos' and 'next' buttons
foreach($pagination as $k => $p){
   $exp1 = explode('">', $p);
   $exp2 = explode('</', $exp1[1]);
   $arr = array(
      0 => $exp1[0].'">',
      1 => is_numeric($exp2[0]) ? $c_pages - (intval($exp2[0])-1) : $exp2[0], //3-2-1 to 1-2-3
      2 => '</'.$exp2[1]
   );
   
   //glue the pieces back, placing the line into either one of the two arrays                     
   if(is_numeric($exp2[0])){
      $links[$k] = implode('',$arr);
   } else {
      $btns[] = implode('',$arr);
   }
 }
    
//add 'previous' and 'next' buttons
//we have to change their places too, so next leads to older (previous) pages
//and account for the cases when there's just one button
if($c_page == 1){
   $btns = array(
       0 => !empty($btns[0]) ? $btns[0] : '',
       1 => ''
   );
}
    
elseif($c_page == $c_pages){
   $btns = array(
      0 => '',
      1 => !empty($btns[0]) ? $btns[0] : ''
   );
    
} else {
   $btns = array(
      0 => !empty($btns[1]) ? $btns[1] : '',
      1 => !empty($btns[0]) ? $btns[0] : ''
   );
}
     
echo $btns[0].implode('',array_reverse($links)).$btns[1];

本文标签: Comments pagination reverse JUST the links texts (123 to 321)not comments order