2012-03-16 78 views
0

我使用此代碼爲我的自定義分頁自定義分頁:PHP:在WordPress的

global $wpdb, $table_prefix, $current_user; 
get_currentuserinfo(); 
$umail = $current_user->user_email; 
$paged = $wpdb->get_results("SELECT * FROM {$table_prefix}comments WHERE comment_author_email = '$umail'"); 

$page = isset($_GET['page']) ? (int) $_GET['page'] : 1; 
$pages = COUNT($paged); 
$pages = ceil($pages/2); 

$querystring = ""; 
foreach ($_GET as $key => $value) { 
    if ($key != "page") $querystring .= "$key=$value&"; 
} 

// Pagination 
for ($i = 1; $i <= $pages; $i++) { 
    echo "<a " . ($i == $page ? "class=\"selected\" " : ""); 
    echo "href=\"?{$querystring}page=$i"; 
    echo "\">$i</a> "; 
} 

此代碼拼版我的意見是這樣的:1 2 3 4 5 6 7 8 9 10 11

如何可以更改代碼來得到分頁看起來像這樣:1 2 3 ... 11

感謝您的任何幫助。

+0

如果當前頁面是7什麼將被顯示:1 2 3 ... 7 8 ... 11或另一個? – Mikhail 2012-03-16 17:23:19

+0

是的,大部分是1 2 3 ... 7 8 ... 11 – Nulled 2012-03-16 18:27:13

回答

2

請嘗試以下循環:

$page   = 8;//current page 
$pages   = 15;//count of all pages 

$batch_middle = 5;//the approximate number of pages in the middle links to display. Current page will be in the middle 
$batch_lr  = 1;//number of pages in the left and right links 
for($i = 1; $i <= $pages;$i++) 
{ 
    //display first links 
    if ($i <= $batch_lr) 
    { 
     echo $i . ' '; 
     continue; 
    } 

    //display last links 
    if ($i > $pages-$batch_lr) 
    { 
     echo $i . ' '; 
     continue; 
    } 

    //display middle links 
    if ($i>=($page-floor($batch_middle/2)) && $i<=($page+floor($batch_middle/2))) 
    { 
     echo $i . ' '; 
     continue; 
    } 

    //placeholder 
    echo ' ... '; 

    //move the pointer 
    $i = ($i < $page) ? ($page-floor($batch_middle/2)-1) : ($pages-$batch_lr) ; 
} 
//output example 1: 1 2 ... 14 15 
//output example 2: 1 2 ... 7 8 9 ... 14 15 
//output example 3: 1 2 3 4 5 ... 14 15 
//output example 4: 1 ... 6 7 8 9 10 ... 15 

更換$page$pages用你的邏輯與得到的當前頁和計數總頁數。
更換$batch_middle$batch_lr配置在連桿的第一,第二和第三批次數量的鏈路

+0

不錯的作品,對於最後一個,怎麼改變這個方法? '1 ... 3 4 5 6 7 ... 15' – Nulled 2012-03-16 18:41:18

+0

爲了得到像top這樣的結果,必須編輯女巫行嗎?我改變了$批次但沒有機會。 – Nulled 2012-03-17 07:21:09

+1

我在回答中更新了我的循環。 – Mikhail 2012-03-18 09:39:01