2009-03-12

WordPress分页代码

Views: 23526 | 2 Comments

将下面的函数放到你的主题的 functions.php 文件中:

function theme_echo_pagenavi(){
    global $wp_query;
    global $request, $posts_per_page, $wpdb, $paged;
    $maxButtonCount = 9; //显示的最多链接数目

    $current_page = $paged;

    $current_page = $paged;
    if(empty($current_page)) {
        $current_page = 1;
    }

    $numposts = $wp_query->found_posts;
    $max_page = $wp_query->max_num_pages;

    $start = max(1, $current_page - intval($maxButtonCount/2));
    $end = min($start + $maxButtonCount - 1, $max_page);
    $start = max(1, $end - $maxButtonCount + 1);

    if($current_page <= 1){
        echo "<span>|<</span>";
        echo "<span><<</span>";
    }else{
        echo '<a href="'.get_pagenum_link().'"><span>|<</span></a>';
        echo '<a href="'.get_pagenum_link($current_page-1).'"><span><<</span></a>';
    }
    for($i=$start; $i<=$end; $i++){
        if($i == $current_page) {
            echo "<span class=\"page_num on\">$i</span>";
        } else {
            echo '<a href="'.get_pagenum_link($i).'"><span class="page_num">'.$i.'</span></a>';
        }
    }
    if($current_page >= $max_page){
        echo "<span>>></span>";
        echo "<span>>|</span> ";
    }else{
        echo '<a href="'.get_pagenum_link($current_page+1).'"><span>>></span></a>';
        echo '<a href="'.get_pagenum_link($max_page).'"><span>>|</span></a>';
    }

    echo " {$current_page}/{$max_page}页, {$numposts}条记录.";
}

在主题的 index.php 文件中这样引用:

<?php theme_echo_pagenavi(); ?>

Related posts:

  1. if-else对优化代码冗余度的反作用
  2. Nginx + PHP 配置和启动脚本
  3. 3行代码的分页算法(求起始页和结束页)
  4. PHP浮点数显示和转成字符串
Posted by ideawu at 2009-03-12 14:47:01 Tags:

2 Responses to "WordPress分页代码"

Leave a Comment