-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathloop-pagination.php
113 lines (96 loc) · 4.09 KB
/
loop-pagination.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Loop Pagination - A WordPress script for creating paginated links on archive-type pages.
*
* The Loop Pagination script was designed to give theme authors a quick way to paginate archive-type
* (archive, search, and blog) pages without having to worry about which of the many plugins a user might
* possibly be using. Instead, they can simply build pagination right into their themes.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @package LoopPagination
* @version 0.3.0
* @author Justin Tadlock <[email protected]>
* @copyright Copyright (c) 2010 - 2013, Justin Tadlock
* @link http://themehybrid.com/docs/tutorials/loop-pagination
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/**
* Loop pagination function for paginating loops with multiple posts. This should be used on archive, blog, and
* search pages. It is not for singular views.
*
* @since 0.1.0
* @access public
* @uses paginate_links() Creates a string of paginated links based on the arguments given.
* @param array $args Arguments to customize how the page links are output.
* @return string $page_links
*/
function loop_pagination( $args = array() ) {
global $wp_rewrite, $wp_query;
/* If there's not more than one page, return nothing. */
if ( 1 >= $wp_query->max_num_pages )
return;
/* Get the current page. */
$current = ( get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1 );
/* Get the max number of pages. */
$max_num_pages = intval( $wp_query->max_num_pages );
/* Get the pagination base. */
$pagination_base = $wp_rewrite->pagination_base;
/* Set up some default arguments for the paginate_links() function. */
$defaults = array(
'base' => add_query_arg( 'paged', '%#%' ),
'format' => '',
'total' => $max_num_pages,
'current' => $current,
'prev_next' => true,
//'prev_text' => __( '« Previous' ), // This is the WordPress default.
//'next_text' => __( 'Next »' ), // This is the WordPress default.
'show_all' => false,
'end_size' => 1,
'mid_size' => 1,
'add_fragment' => '',
'type' => 'plain',
// Begin loop_pagination() arguments.
'before' => '<nav class="pagination loop-pagination">',
'after' => '</nav>',
'echo' => true,
);
/* Add the $base argument to the array if the user is using permalinks. */
if ( $wp_rewrite->using_permalinks() && !is_search() )
$defaults['base'] = user_trailingslashit( trailingslashit( get_pagenum_link() ) . "{$pagination_base}/%#%" );
/* Allow developers to overwrite the arguments with a filter. */
$args = apply_filters( 'loop_pagination_args', $args );
/* Merge the arguments input with the defaults. */
$args = wp_parse_args( $args, $defaults );
/* Don't allow the user to set this to an array. */
if ( 'array' == $args['type'] )
$args['type'] = 'plain';
/* Get the paginated links. */
$page_links = paginate_links( $args );
/* Remove 'page/1' from the entire output since it's not needed. */
$page_links = preg_replace(
array(
"#(href=['\"].*?){$pagination_base}/1(['\"])#", // 'page/1'
"#(href=['\"].*?){$pagination_base}/1/(['\"])#", // 'page/1/'
"#(href=['\"].*?)\?paged=1(['\"])#", // '?paged=1'
"#(href=['\"].*?)&\#038;paged=1(['\"])#" // '&paged=1'
),
'$1$2',
$page_links
);
/* Wrap the paginated links with the $before and $after elements. */
$page_links = $args['before'] . $page_links . $args['after'];
/* Allow devs to completely overwrite the output. */
$page_links = apply_filters( 'loop_pagination', $page_links );
/* Return the paginated links for use in themes. */
if ( $args['echo'] )
echo $page_links;
else
return $page_links;
}
?>