給 WordPress 添加數字分頁導航

製做 BlueNight 主題的工程中積累一些 WordPress 的開發經驗,今天這裏就把如何給 WordPress 添加數字分頁導航的 PHP 代碼給出: php

<?php
/**
 * Paginate
 */
if (!function_exists('paginate')):
    function paginate($args = null) {
        // 顯示數字當行個數的範圍基數:默認值 = 3
        $range_gap = 3; 
        
        // 主題設置的值,若是你不是使用的 BlueNight 主題,那麼有 $range_gap = 3; 就OK了
        if (get_option('bluenight_paginate_num') != '' && intval(get_option('bluenight_paginate_num')) > 0) {
            $range_gap = intval(get_option('bluenight_paginate_num'));
        }
        
        // 默認的分頁的搜索查詢參數
        $defaults = array('page'=>null, 'pages'=>null, 'range'=>$range_gap, 'gap'=>$range_gap, 'anchor'=>1, 'echo'=>1);
        
        $r = wp_parse_args($args, $defaults);
        extract($r, EXTR_SKIP);
        
        if (!$page && !$pages) {
            global $wp_query;
            
            // 得到當前頁數
            $page = get_query_var('paged');
            $page = ! empty($page) ? intval($page) : 1;
            
            // 每頁顯示多少篇文章
            $posts_per_page = intval(get_query_var('posts_per_page'));
            // 計算導航的總頁數
            $pages = intval(ceil($wp_query->found_posts / $posts_per_page));
        }
        
        $output = "";
        if ($pages > 1) {
            $ellipsis = "<span class='paginate-gap'>...</span>";
            
            // 當前數字頁座右顯示的數字導航的返回,大於這個值(這裏是7),就顯示 $ellipsis 省略號
            $min_links = $range * 2 + 1;
            $block_min = min($page - $range, $pages - $min_links);
            $block_high = max($page + $range, $min_links);
            $left_gap = (($block_min - $anchor - $gap) > 0) ? true : false;
            $right_gap = (($block_high + $anchor + $gap) < $pages) ? true : false;
            
            if ($left_gap && !$right_gap) {
                $output .= sprintf('%s%s%s', paginate_loop(1, $anchor), $ellipsis, paginate_loop($block_min, $pages, $page));
            } else if ($left_gap && $right_gap) {
                $output .= sprintf('%s%s%s%s%s', paginate_loop(1, $anchor), $ellipsis, paginate_loop($block_min, $block_high, $page), $ellipsis, paginate_loop(($pages - $anchor + 1), $pages));
            } else if ($right_gap && !$left_gap) {
                $output .= sprintf('%s%s%s', paginate_loop(1, $block_high, $page), $ellipsis, paginate_loop(($pages - $anchor + 1), $pages));
            } else {
                $output .= paginate_loop(1, $pages, $page);
            }
        }
        
        if ($echo) {
            echo $output;
        }
        
        return $output;
    }
endif;


if (!function_exists('paginate_loop')):
    function paginate_loop($start, $max, $page = 0) {
        $output = "";
        for ($i = $start; $i <= $max; $i++) {
            $output .= ($page === intval($i)) ? "<span class='paginate-page paginate-current'>$i</span>" : "<a href='".get_pagenum_link($i)."' class='paginate-page'>$i</a>";
        }
        return $output;
    }
endif;


if (!function_exists('show_paginate')):
    function show_paginate() {
        
?>
<div class="paginate">
    <?php
    // 原本是能夠直接在 paginate 函數裏實現上下頁的導航的,
    // 不過 WordPress 要求必須使用 previous_posts_link() 函數作分頁導航
    // 不然連提交代碼都提交不了,因此就這麼拼接了一下導航
    previous_posts_link(__('&laquo; Previous Page', 'bluenight'), 0);
    if (function_exists("paginate")) {
        paginate();
    }
    next_posts_link(__('Next Page &raquo;', 'bluenight'), 0);
    wp_link_pages();
    ?>
</div>
<?php
}
endif;
?>

將這段代碼添加到你的主題的 functions.php 文件中,而後在列表頁 index.php、achive.php、tag.php、author.php、category.php、search.php 等頁面(有的主題沒有這麼多列表頁不過index.php,search.php是必定有的)加上: wordpress

// 輸出文章列表
while (have_posts()):
    the_post();
    if (function_exists('show_post_item')) {
        show_post_item();
    }
endwhile;

// 顯示分頁導航
if (function_exists('show_paginate')) {
    show_paginate();
相關文章
相關標籤/搜索