| | 880 | |
| | 881 | function paginate_links( $arg = '' ) { |
| | 882 | if ( is_array($arg) ) |
| | 883 | $a = &$arg; |
| | 884 | else |
| | 885 | parse_str($arg, $a); |
| | 886 | |
| | 887 | // Defaults |
| | 888 | $base = '%_%'; // http://example.com/all_posts.php%_% : %_% is replaced by format (below) |
| | 889 | $format = '?page=%#%'; // ?page=%#% : %#% is replaced by the page number |
| | 890 | $total = 1; |
| | 891 | $current = 0; |
| | 892 | $show_all = false; |
| | 893 | $prev_next = true; |
| | 894 | $prev_text = __('« Previous'); |
| | 895 | $next_text = __('Next »'); |
| | 896 | $end_size = 1; // How many numbers on either end including the end |
| | 897 | $mid_size = 2; // How many numbers to either side of current not including current |
| | 898 | $type = 'plain'; |
| | 899 | $add_args = false; // array of query args to aadd |
| | 900 | |
| | 901 | extract($a); |
| | 902 | |
| | 903 | // Who knows what else people pass in $args |
| | 904 | $total = (int) $total; |
| | 905 | $current = (int) $current; |
| | 906 | $end_size = 0 < (int) $end_size ? (int) $end_size : 1; // Out of bounds? Make it the default. |
| | 907 | $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2; |
| | 908 | $add_args = is_array($add_args) ? $add_args : false; |
| | 909 | $r = ''; |
| | 910 | $page_links = array(); |
| | 911 | $n = 0; |
| | 912 | $dots = false; |
| | 913 | |
| | 914 | if ( $prev_next && $current && 1 < $current ) : |
| | 915 | $link = str_replace('%_%', 2 == $current ? '' : str_replace('%#%', $current - 1, $format), $base); |
| | 916 | if ( $add_args ) |
| | 917 | $link = add_query_arg( $add_args, $link ); |
| | 918 | $page_links[] = "<a class='prev page-numbers' href='" . wp_specialchars( $link, 1 ) . "'>$prev_text</a>"; |
| | 919 | endif; |
| | 920 | for ( $n = 1; $n <= $total; $n++ ) : |
| | 921 | if ( $n == $current ) : |
| | 922 | $page_links[] = "<span class='page-numbers current'>$n</span>"; |
| | 923 | $dots = true; |
| | 924 | else : |
| | 925 | if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) : |
| | 926 | $link = str_replace('%_%', 1 == $n ? '' : str_replace('%#%', $n, $format), $base); |
| | 927 | if ( $add_args ) |
| | 928 | $link = add_query_arg( $add_args, $link ); |
| | 929 | $page_links[] = "<a class='page-numbers' href='" . wp_specialchars( $link, 1 ) . "'>$n</a>"; |
| | 930 | $dots = true; |
| | 931 | elseif ( $dots && !$show_all ) : |
| | 932 | $page_links[] = "<span class='page-numbers dots'>...</span>"; |
| | 933 | $dots = false; |
| | 934 | endif; |
| | 935 | endif; |
| | 936 | endfor; |
| | 937 | if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) : |
| | 938 | $link = str_replace('%_%', str_replace('%#%', $current + 1, $format), $base); |
| | 939 | if ( $add_args ) |
| | 940 | $link = add_query_arg( $add_args, $link ); |
| | 941 | $page_links[] = "<a class='next page-numbers' href='" . wp_specialchars( $link, 1 ) . "'>$next_text</a>"; |
| | 942 | endif; |
| | 943 | switch ( $type ) : |
| | 944 | case 'array' : |
| | 945 | return $page_links; |
| | 946 | break; |
| | 947 | case 'list' : |
| | 948 | $r .= "<ul class='page-numbers'>\n\t<li>"; |
| | 949 | $r .= join("</li>\n\t<li>", $page_links); |
| | 950 | $r .= "</li>\n</ul>\n"; |
| | 951 | break; |
| | 952 | default : |
| | 953 | $r = join("\n", $page_links); |
| | 954 | break; |
| | 955 | endswitch; |
| | 956 | return $r; |
| | 957 | } |