Skip to content

Instantly share code, notes, and snippets.

@mtx-z
Last active January 16, 2025 16:15
Show Gist options
  • Save mtx-z/f95af6cc6fb562eb1a1540ca715ed928 to your computer and use it in GitHub Desktop.
Save mtx-z/f95af6cc6fb562eb1a1540ca715ed928 to your computer and use it in GitHub Desktop.

Revisions

  1. mtx-z revised this gist May 1, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion wp-bootstrap4.4-pagination.php
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,11 @@
    *
    * @return string|null
    *
    * UPDATE for Bootstrap 5.0: https://gist.github.com/mtx-z/af85d3abd4c19a84a9713e69956e1507
    *
    * Accepts a WP_Query instance to build pagination (for custom wp_query()),
    * or nothing to use the current global $wp_query (eg: taxonomy term page)
    * - Tested on WP 5.4.1
    * - Tested on WP 5.7.1
    * - Tested with Bootstrap 4.4
    * - Tested on Sage 9.0.9
    *
  2. mtx-z revised this gist Jun 9, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wp-bootstrap4.4-pagination.php
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@
    * echo bootstrap_pagination($query);
    * ?>
    */
    function bootstrap_paginationtest( \WP_Query $wp_query = null, $echo = true, $params = [] ) {
    function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true, $params = [] ) {
    if ( null === $wp_query ) {
    global $wp_query;
    }
  3. mtx-z revised this gist May 20, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions wp-bootstrap4.4-pagination.php
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    * @param array $params
    *
    * @return string|null
    *
    * Accepts a WP_Query instance to build pagination (for custom wp_query()),
    * or nothing to use the current global $wp_query (eg: taxonomy term page)
    * - Tested on WP 5.4.1
  4. mtx-z revised this gist May 20, 2020. 1 changed file with 71 additions and 42 deletions.
    113 changes: 71 additions & 42 deletions wp-bootstrap4.4-pagination.php
    Original file line number Diff line number Diff line change
    @@ -1,65 +1,94 @@
    <?php

    <?php
    /**
    * @param WP_Query|null $wp_query
    * @param bool $echo
    * @param array $params
    *
    * @return string
    * Accepts a WP_Query instance to build pagination (for custom wp_query()),
    * @return string|null
    * Accepts a WP_Query instance to build pagination (for custom wp_query()),
    * or nothing to use the current global $wp_query (eg: taxonomy term page)
    * - Tested on WP 5.3
    * - Tested on WP 5.4.1
    * - Tested with Bootstrap 4.4
    * - Tested on Sage 9.0.9
    *
    * INSTALLATION:
    * add this file content to your theme function.php or equivalent
    *
    * USAGE:
    * <?php echo bootstrap_pagination(); ?> //uses global $wp_query
    * or with custom WP_Query():
    * <?php
    * <?php
    * $query = new \WP_Query($args);
    * ... while(have_posts()), $query->posts stuff ...
    * ... while(have_posts()), $query->posts stuff ... endwhile() ...
    * echo bootstrap_pagination($query);
    * ?>
    */
    function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true ) {
    function bootstrap_paginationtest( \WP_Query $wp_query = null, $echo = true, $params = [] ) {
    if ( null === $wp_query ) {
    global $wp_query;
    }

    if ( null === $wp_query ) {
    global $wp_query;
    }
    $add_args = [];

    $pages = paginate_links( [
    'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var( 'paged' ) ),
    'total' => $wp_query->max_num_pages,
    'type' => 'array',
    'show_all' => false,
    'end_size' => 3,
    'mid_size' => 1,
    'prev_next' => true,
    'prev_text' => __( '« Prev' ),
    'next_text' => __( 'Next »' ),
    'add_args' => false,
    'add_fragment' => ''
    ]
    );
    //add query (GET) parameters to generated page URLs
    /*if (isset($_GET[ 'sort' ])) {
    $add_args[ 'sort' ] = (string)$_GET[ 'sort' ];
    }*/

    if ( is_array( $pages ) ) {
    //$paged = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' );
    $pages = paginate_links( array_merge( [
    'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var( 'paged' ) ),
    'total' => $wp_query->max_num_pages,
    'type' => 'array',
    'show_all' => false,
    'end_size' => 3,
    'mid_size' => 1,
    'prev_next' => true,
    'prev_text' => __( '« Prev' ),
    'next_text' => __( 'Next »' ),
    'add_args' => $add_args,
    'add_fragment' => ''
    ], $params )
    );

    $pagination = '<div class="pagination"><ul class="pagination">';
    if ( is_array( $pages ) ) {
    //$current_page = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' );
    $pagination = '<div class="pagination"><ul class="pagination">';

    foreach ($pages as $page) {
    $pagination .= '<li class="page-item' . (strpos($page, 'current') !== false ? ' active' : '') . '"> ' . str_replace('page-numbers', 'page-link', $page) . '</li>';
    }
    foreach ( $pages as $page ) {
    $pagination .= '<li class="page-item' . (strpos($page, 'current') !== false ? ' active' : '') . '"> ' . str_replace('page-numbers', 'page-link', $page) . '</li>';
    }

    $pagination .= '</ul></div>';
    $pagination .= '</ul></div>';

    if ( $echo ) {
    echo $pagination;
    } else {
    return $pagination;
    }
    }
    if ( $echo ) {
    echo $pagination;
    } else {
    return $pagination;
    }
    }

    return null;
    }
    return null;
    }

    /**
    * Notes:
    * AJAX:
    * - When used with wp_ajax (generate pagination HTML from ajax) you'll need to provide base URL (or it'll be admin-ajax URL)
    * - Example for a term page: bootstrap_pagination( $query, false, ['base' => get_term_link($term) . '?paged=%#%'] )
    *
    * Images as next/prev:
    * - You can use image as next/prev buttons
    * - Example: 'prev_text' => '<img src="' . get_stylesheet_directory_uri() . '/assets/images/prev-arrow.svg">',
    *
    * Add query parameters to page URLs
    * - If you need custom URL parameters on your page URLS, use the "add_args" attribute
    * - Example (before paginate_links() call):
    * $arg = [];
    * if (isset($_GET[ 'sort' ])) {
    * $args[ 'sort' ] = (string)$_GET[ 'sort' ];
    * }
    * ...
    * 'add_args' => $args,
    */
  5. mtx-z renamed this gist Dec 11, 2019. 1 changed file with 3 additions and 3 deletions.
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,9 @@
    * @return string
    * Accepts a WP_Query instance to build pagination (for custom wp_query()),
    * or nothing to use the current global $wp_query (eg: taxonomy term page)
    * - Tested on WP 4.9.5
    * - Tested with Bootstrap 4.1
    * - Tested on Sage 9
    * - Tested on WP 5.3
    * - Tested with Bootstrap 4.4
    * - Tested on Sage 9.0.9
    *
    * USAGE:
    * <?php echo bootstrap_pagination(); ?> //uses global $wp_query
  6. mtx-z revised this gist Jun 28, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions wp-bootstrap4.1-pagination.php
    Original file line number Diff line number Diff line change
    @@ -48,9 +48,9 @@ function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true ) {

    $pagination = '<div class="pagination"><ul class="pagination">';

    foreach ( $pages as $page ) {
    $pagination .= '<li class="page-item'.(strpos($page, 'current') !== false ? ' active' : '').'"> ' . str_replace( 'page-numbers', 'page-link', $page ) . '</li>';
    }
    foreach ($pages as $page) {
    $pagination .= '<li class="page-item' . (strpos($page, 'current') !== false ? ' active' : '') . '"> ' . str_replace('page-numbers', 'page-link', $page) . '</li>';
    }

    $pagination .= '</ul></div>';

  7. mtx-z revised this gist Jun 28, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wp-bootstrap4.1-pagination.php
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true ) {
    $pagination = '<div class="pagination"><ul class="pagination">';

    foreach ( $pages as $page ) {
    $pagination .= '<li class="page-item"> ' . str_replace( 'page-numbers', 'page-link', $page ) . '</li>';
    $pagination .= '<li class="page-item'.(strpos($page, 'current') !== false ? ' active' : '').'"> ' . str_replace( 'page-numbers', 'page-link', $page ) . '</li>';
    }

    $pagination .= '</ul></div>';
  8. mtx-z revised this gist Apr 15, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions wp-bootstrap4.1-pagination.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    /**
    * @param WP_Query|null $wp_query
    * @param bool $echo
    @@ -8,6 +10,7 @@
    * - Tested on WP 4.9.5
    * - Tested with Bootstrap 4.1
    * - Tested on Sage 9
    *
    * USAGE:
    * <?php echo bootstrap_pagination(); ?> //uses global $wp_query
    * or with custom WP_Query():
  9. mtx-z revised this gist Apr 15, 2018. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions wp-bootstrap4.1-pagination.php
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,14 @@
    * - Tested on WP 4.9.5
    * - Tested with Bootstrap 4.1
    * - Tested on Sage 9
    * USAGE:
    * <?php echo bootstrap_pagination(); ?> //uses global $wp_query
    * or with custom WP_Query():
    * <?php
    * $query = new \WP_Query($args);
    * ... while(have_posts()), $query->posts stuff ...
    * echo bootstrap_pagination($query);
    * ?>
    */
    function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true ) {

  10. mtx-z revised this gist Apr 15, 2018. No changes.
  11. mtx-z revised this gist Apr 15, 2018. 1 changed file with 20 additions and 15 deletions.
    35 changes: 20 additions & 15 deletions wp-bootstrap4.1-pagination.php
    Original file line number Diff line number Diff line change
    @@ -3,39 +3,42 @@
    * @param bool $echo
    *
    * @return string
    * This accept a WP_Query instance to build pagination (for custom wp_query()),
    * Accepts a WP_Query instance to build pagination (for custom wp_query()),
    * or nothing to use the current global $wp_query (eg: taxonomy term page)
    * - Tested on WP 4.9.5
    * - Tested with Bootstrap 4.1
    * - Tested on Sage 9
    */
    function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true) {
    function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true ) {

    if(null === $wp_query){
    if ( null === $wp_query ) {
    global $wp_query;
    }

    $pages = paginate_links( array(
    $pages = paginate_links( [
    'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages,
    'type' => 'array',
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var( 'paged' ) ),
    'total' => $wp_query->max_num_pages,
    'type' => 'array',
    'show_all' => false,
    'end_size' => 3,
    'mid_size' => 1,
    'prev_next' => true,
    'prev_text' => __('« Prev'),
    'next_text' => __('Next »'),
    'prev_next' => true,
    'prev_text' => __( '« Prev' ),
    'next_text' => __( 'Next »' ),
    'add_args' => false,
    'add_fragment' => ''
    )
    ]
    );

    if( is_array( $pages ) ) {
    //$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
    if ( is_array( $pages ) ) {
    //$paged = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' );

    $pagination = '<div class="pagination"><ul class="pagination">';

    foreach ( $pages as $page ) {
    $pagination .= '<li class="page-item"> ' . str_replace('page-numbers', 'page-link', $page) . '</li>';
    $pagination .= '<li class="page-item"> ' . str_replace( 'page-numbers', 'page-link', $page ) . '</li>';
    }

    $pagination .= '</ul></div>';
    @@ -46,4 +49,6 @@ function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true) {
    return $pagination;
    }
    }

    return null;
    }
  12. mtx-z revised this gist Apr 15, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wp-bootstrap4.1-pagination.php
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true) {
    );

    if( is_array( $pages ) ) {
    $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
    //$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');

    $pagination = '<div class="pagination"><ul class="pagination">';

  13. mtx-z revised this gist Apr 15, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion wp-bootstrap4.1-pagination.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,8 @@
    * @param bool $echo
    *
    * @return string
    * This accept a WP_Query instance to build pagination (for custom wp_query()), or nothing to use the current global $wp_query (eg: taxonomy term page)
    * This accept a WP_Query instance to build pagination (for custom wp_query()),
    * or nothing to use the current global $wp_query (eg: taxonomy term page)
    */
    function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true) {

  14. mtx-z renamed this gist Apr 15, 2018. 1 changed file with 1 addition and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    * @param bool $echo
    *
    * @return string
    * This accept a WP_Query instance to build pagination (for custom wp_query()), or nothing to use the current global $wp_query (eg: taxonomy term page)
    */
    function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true) {

  15. mtx-z created this gist Apr 15, 2018.
    47 changes: 47 additions & 0 deletions bootstrap-pagination.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    /**
    * @param WP_Query|null $wp_query
    * @param bool $echo
    *
    * @return string
    */
    function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true) {

    if(null === $wp_query){
    global $wp_query;
    }

    $pages = paginate_links( array(
    'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages,
    'type' => 'array',
    'show_all' => false,
    'end_size' => 3,
    'mid_size' => 1,
    'prev_next' => true,
    'prev_text' => __('« Prev'),
    'next_text' => __('Next »'),
    'add_args' => false,
    'add_fragment' => ''
    )
    );

    if( is_array( $pages ) ) {
    $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');

    $pagination = '<div class="pagination"><ul class="pagination">';

    foreach ( $pages as $page ) {
    $pagination .= '<li class="page-item"> ' . str_replace('page-numbers', 'page-link', $page) . '</li>';
    }

    $pagination .= '</ul></div>';

    if ( $echo ) {
    echo $pagination;
    } else {
    return $pagination;
    }
    }
    }