在WordPress中显示数字分页与上一个和下一个链接?

时间:2010-08-27 作者:Gelatin

默认情况下,WordPress显示previousnext 内容拆分到不同页面时的链接。有没有办法让它使用正确的分页(带有数字),或者使用插件?

2 个回复
最合适的回答,由SO网友:Sergio Soares 整理而成
SO网友:Chris_O

Add to Functions.php

/* Numeric Pagination ********************************************/

function numeric_pagination ($pageCount = 9, $query = null) {

    if ($query == null) {
        global $wp_query;
        $query = $wp_query;
    }

    if ($query->max_num_pages <= 1) {
        return;
    }

    $pageStart = 1;
    $paged = $query->query_vars[\'paged\'];

    // set current page if on the first page
    if ($paged == null) {
        $paged = 1;
    }

    // work out if page start is halfway through the current visible pages and if so move it accordingly
    if ($paged > floor($pageCount / 2)) {
        $pageStart = $paged - floor($pageCount / 2);
    }

    if ($pageStart < 1) {
        $pageStart = 1;
    }

    // make sure page start is
    if ($pageStart + $pageCount > $query->max_num_pages) {
        $pageCount = $query->max_num_pages - $pageStart;
    }

?>
    <div id="archive_pagination">
<?php
    if ($paged != 1) {
?>
    <a href="<?php echo get_pagenum_link(1); ?>" class="numbered page-number-first"><span>&lsaquo; <?php _e(\'<< First\', \'global\'); ?></span></a>
<?php
    }
    // first page is not visible...
    if ($pageStart > 1) {
        //echo \'previous\';
    }
    for ($p = $pageStart; $p <= $pageStart + $pageCount; $p ++) {
        if ($p == $paged) {
?>
        <span class="numbered page-number-<?php echo $p; ?> current-numeric-page"><?php echo $p; ?></span>
<?php } else { ?>
        <a href="<?php echo get_pagenum_link($p); ?>" class="numbered page-number-<?php echo $p; ?>"><span><?php echo $p; ?></span></a>

<?php
        }
    }
    // last page is not visible
    if ($pageStart + $pageCount < $query->max_num_pages) {
        //echo "last";
    }
    if ($paged != $query->max_num_pages) {
?>
        <a href="<?php echo get_pagenum_link($query->max_num_pages); ?>" class="numbered page-number-last"><span><?php _e(\'>> Last\', \'global\'); ?> &rsaquo;</span></a>
<?php } ?>

    </div>

<?php } ?>

Before The Loop

<?php
$page = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
query_posts("showposts=6&paged=$page");
?>

After The Loop Between endwhile and else

<?php numeric_pagination(); ?> 

相关推荐

Count posts for pagination

我正在为一个网站分页<;上一页(页码)下一页>很简单,已经完成。但是现在我需要添加一个选择器来直接转到页面(例如:转到第7页),要这样做,我需要知道有多少页面,为此我需要计算在查询中找到了多少帖子。问题是这个网站有太多的帖子(>13.000),查询所有帖子都会减慢页面加载速度,这就像。。。10秒后页面才能加载。显然,这是不可接受的。分页解决了这个问题,因为一次只加载50或100篇文章,但我无法将它们全部计算在内。我可以在不加载的情况下统计某个查询中的帖子吗?或者我可以通过其他方式获得页数吗

在WordPress中显示数字分页与上一个和下一个链接? - 小码农CODE - 行之有效找到问题解决它

在WordPress中显示数字分页与上一个和下一个链接?

时间:2010-08-27 作者:Gelatin

默认情况下,WordPress显示previousnext 内容拆分到不同页面时的链接。有没有办法让它使用正确的分页(带有数字),或者使用插件?

2 个回复
最合适的回答,由SO网友:Sergio Soares 整理而成
SO网友:Chris_O

Add to Functions.php

/* Numeric Pagination ********************************************/

function numeric_pagination ($pageCount = 9, $query = null) {

    if ($query == null) {
        global $wp_query;
        $query = $wp_query;
    }

    if ($query->max_num_pages <= 1) {
        return;
    }

    $pageStart = 1;
    $paged = $query->query_vars[\'paged\'];

    // set current page if on the first page
    if ($paged == null) {
        $paged = 1;
    }

    // work out if page start is halfway through the current visible pages and if so move it accordingly
    if ($paged > floor($pageCount / 2)) {
        $pageStart = $paged - floor($pageCount / 2);
    }

    if ($pageStart < 1) {
        $pageStart = 1;
    }

    // make sure page start is
    if ($pageStart + $pageCount > $query->max_num_pages) {
        $pageCount = $query->max_num_pages - $pageStart;
    }

?>
    <div id="archive_pagination">
<?php
    if ($paged != 1) {
?>
    <a href="<?php echo get_pagenum_link(1); ?>" class="numbered page-number-first"><span>&lsaquo; <?php _e(\'<< First\', \'global\'); ?></span></a>
<?php
    }
    // first page is not visible...
    if ($pageStart > 1) {
        //echo \'previous\';
    }
    for ($p = $pageStart; $p <= $pageStart + $pageCount; $p ++) {
        if ($p == $paged) {
?>
        <span class="numbered page-number-<?php echo $p; ?> current-numeric-page"><?php echo $p; ?></span>
<?php } else { ?>
        <a href="<?php echo get_pagenum_link($p); ?>" class="numbered page-number-<?php echo $p; ?>"><span><?php echo $p; ?></span></a>

<?php
        }
    }
    // last page is not visible
    if ($pageStart + $pageCount < $query->max_num_pages) {
        //echo "last";
    }
    if ($paged != $query->max_num_pages) {
?>
        <a href="<?php echo get_pagenum_link($query->max_num_pages); ?>" class="numbered page-number-last"><span><?php _e(\'>> Last\', \'global\'); ?> &rsaquo;</span></a>
<?php } ?>

    </div>

<?php } ?>

Before The Loop

<?php
$page = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
query_posts("showposts=6&paged=$page");
?>

After The Loop Between endwhile and else

<?php numeric_pagination(); ?> 

相关推荐

change pagination url

目前,我的分页页面URL为:http://www.example.com/category_name/page/2但我需要将此URL结构更改为:http://www.example.com/category_name/?page=2等有什么解决方案吗?