wordpress numeric pagination with bootstrap

Post Reply
User avatar
hiccup
Site Admin
Posts: 20
Joined: Sat Aug 01, 2015 8:44 pm

wordpress numeric pagination with bootstrap

Post by hiccup » Mon Nov 02, 2015 6:36 am

the query: Static front page

Code: php Select all

$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
  'post_type' => 'post_type',
  'category_name' => 'tutorials',
  'posts_per_page' => 5,
  'paged' => $paged
);
$the_query = new WP_Query( $args );
static page that is NOT home:

Code: php Select all

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
  'post_type' => 'post_type',
  'category_name' => 'tutorials',
  'posts_per_page' => 5,
  'paged' => $paged
);
$the_query = new WP_Query( $args );
Pagination Note: Use get_query_var(‘page’); if you want your query to work in a Page template that you’ve set as your static front page…Display posts from current page on a static front page.

the function:

Code: php Select all

function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }

  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  /*pagination arguments for paginate_links function. */

  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('«'),
    'next_text'       => __('»'),
    'type'            => 'array',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);
  echo "<li class='page-numbers page-num'><a>Page " . $paged . " of " . $numpages . "</a></li> ";

   foreach ( $paginate_links as $pgl ) {
        echo "<li> $pgl </li>";
    }

}
Reference: paginate_links

custom page that is NOT home:

Code: php Select all

<div class="col-md-12">
    <?php
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    $args = array('post_type'=> 'post','posts_per_page' => 10, 'paged' => $paged,);
    $myposts = new WP_Query( $args );
    if ($myposts->have_posts() ){
    while( $myposts->have_posts() ) : $myposts->the_post(); ?>			
        <!-- content -->
    <?php endwhile; ?>
    <?php
        if ($myposts->max_num_pages > 1){ ?>
        <nav class="pull-right">
            <ul class="pagination">
         <?php
            if (function_exists(custom_pagination)) {
            custom_pagination($myposts->max_num_pages,"",$paged);
            }
        ?>
            </ul>
        </nav>
    <?php }} else { ?>
        <p><?php _e( 'Sorry, No notice found.' ); ?></p>
      <?php } wp_reset_postdata(); ?>
</div>
custom front page (Home):

Code: php Select all

<div class="col-md-12">
    <?php
    $paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
    $args = array('post_type'=> 'post','posts_per_page' => 5, 'page' => $paged,);
    $myposts = new WP_Query( $args );
    if ($myposts->have_posts() ){
    while( $myposts->have_posts() ) : $myposts->the_post(); ?>			
        <!-- content -->
    <?php endwhile; ?>
    <nav>
        <ul class="pagination">
     <?php
        if (function_exists(custom_pagination)) {
        custom_pagination($myposts->max_num_pages,"",$paged);
        }
    ?>
        </ul>
    </nav>
    <?php } else { ?>
        <p><?php _e( 'Sorry, No post found.' ); ?></p>
      <?php } wp_reset_postdata(); ?>
</div>
source: 1 2

Post Reply