PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/news/library/extensions/loop-pagination.php

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