Timber\Pagination::paginate_links PHP Method

    public static function paginate_links($args = array())
    {
        $defaults = array('base' => '%_%', 'format' => '?page=%#%', 'total' => 1, 'current' => 0, 'show_all' => false, 'prev_next' => false, 'prev_text' => __('« Previous'), 'next_text' => __('Next »'), 'end_size' => 1, 'mid_size' => 2, 'type' => 'array', 'add_args' => false, 'add_fragment' => '');
        $args = wp_parse_args($args, $defaults);
        // Who knows what else people pass in $args
        $args['total'] = intval((int) $args['total']);
        if ($args['total'] < 2) {
            return array();
        }
        $args['current'] = (int) $args['current'];
        $args['end_size'] = 0 < (int) $args['end_size'] ? (int) $args['end_size'] : 1;
        // Out of bounds?  Make it the default.
        $args['mid_size'] = 0 <= (int) $args['mid_size'] ? (int) $args['mid_size'] : 2;
        $args['add_args'] = is_array($args['add_args']) ? $args['add_args'] : false;
        $page_links = array();
        $dots = false;
        for ($n = 1; $n <= $args['total']; $n++) {
            $n_display = number_format_i18n($n);
            if ($n == $args['current']) {
                $page_links[] = array('class' => 'page-number page-numbers current', 'title' => $n_display, 'text' => $n_display, 'name' => $n_display, 'current' => true);
                $dots = true;
            } else {
                if ($args['show_all'] || ($n <= $args['end_size'] || $args['current'] && $n >= $args['current'] - $args['mid_size'] && $n <= $args['current'] + $args['mid_size'] || $n > $args['total'] - $args['end_size'])) {
                    $link = str_replace('%_%', 1 == $n ? '' : $args['format'], $args['base']);
                    $link = str_replace('%#%', $n, $link);
                    // we first follow the user trailing slash configuration
                    $link = user_trailingslashit($link);
                    // then we add all required querystring parameters
                    if ($args['add_args']) {
                        $link = add_query_arg($args['add_args'], $link);
                    }
                    // last, we add fragment if needed
                    $link .= $args['add_fragment'];
                    $link = esc_url(apply_filters('paginate_links', $link));
                    $page_links[] = array('class' => 'page-number page-numbers', 'link' => $link, 'title' => $n_display, 'name' => $n_display, 'current' => $args['current'] == $n);
                    $dots = true;
                } elseif ($dots && !$args['show_all']) {
                    $page_links[] = array('class' => 'dots', 'title' => __('&hellip;'));
                    $dots = false;
                }
            }
        }
        return $page_links;
    }

Usage Example

Example #1
0
 function testPaginateLinksWithOutTrailingSlash()
 {
     $args = array('total' => 20);
     $this->setPermalinkStructure('/%year%/%post_id%');
     $pagination = \Timber\Pagination::paginate_links($args);
     foreach ($pagination as $page) {
         if (array_key_exists('link', $page) && !empty($page['link'])) {
             $this->assertFalse(self::endsWith(substr($page['link'], -1), '/'));
         }
     }
 }
All Usage Examples Of Timber\Pagination::paginate_links