Pagination For Page Post Type

时间:2016-03-26 作者:Ari

我尝试使用Eric Meyer的分页代码使其适用于page post类型,但无法确定如何使其工作。它总是显示第一页。

以下是代码:

<?php /* Pagination - Thanx to and (c) 2009 by Eric Martin - License: GNU GENERAL PUBLIC LICENSE */
function paginate($args = null) {
    $defaults = array( 
        \'page\' => null,
        \'pages\' => null, 
        \'range\' => 3,
        \'gap\' => 3, \'anchor\' => 1,
        \'before\' => \'<div class="paginate">\', \'after\' => \'</div>\',
        \'nextpage\' => __(\'&raquo;\'), \'previouspage\' => __(\'&laquo\'),
        \'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) {   
        $output .= "$before";
        $ellipsis = "<span class=\'gap\'>...</span>";
        if ($page > 1 && !empty($previouspage)) {
            $output .= "<a href=\'" . get_pagenum_link($page - 1) . "\' class=\'prev\' >$previouspage</a>";
        }       
        $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 ($page < $pages && !empty($nextpage)) {
            $output .= "<a href=\'" . get_pagenum_link($page + 1) . "\' class=\'next\' >$nextpage</a>";
        }
        $output .= $after;
    }
    if ($echo) {
        echo $output;
    }
    return $output;
}
function paginate_loop($start, $max, $page = 0) {
    $output = "";
    for ($i = $start; $i <= $max; $i++) {
        $output .= ($page === intval($i)) 
            ? "<span class=\'page current\'>$i</span>" 
            : "<a href=\'" . get_pagenum_link($i) . "\' class=\'page paginate\' >$i</a>";
    }
    return $output;
}
?>
有人能给出一个解决方案吗?

1 个回复
SO网友:Tung Du

事实上,我想你不需要像上面这样做

例如,用于检索页面的循环,

$args = array(
    \'posts_per_page\' => 5,
    \'post_type\' => \'page\',
    \'paged\' => $paged,
);

$the_query = new WP_Query( $args );
?>
<!-- the loop etc.. -->
然后获取分页链接

<?php
$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $the_query->max_num_pages //The custom WP_Query instance
) );
?>

相关推荐

Hiding menu on specific pages

我在某些页面上隐藏顶部菜单时遇到问题。以下是我试图隐藏菜单的网站https://domain.com/cookies-statement/ https://domain.com/privacy-policy/ 看来,来自其他线程的解决方案对我不起作用(或者我做错了什么),请告诉我这是否可能与我的主题一起实现谢谢