Advertisement
kobial8

Pagination in Template Page

Dec 23rd, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.64 KB | None | 0 0
  1. 1.  Put this in your template page        
  2.           <?php
  3.           $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  4.            
  5.           $args = array(
  6.              'posts_per_page' => 9,
  7.              'category_name' => 'local-cricket',
  8.              'paged' => $paged
  9.           );
  10.            
  11.           $custom_query = new WP_Query( $args );
  12.            
  13.           while($custom_query->have_posts()) :
  14.              $custom_query->the_post();
  15.            ?>
  16.                 <div class="col-sm-4">
  17.                     <div class="single">
  18.                         <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('category_img', array('class' => 'img-responsive')); ?></a>
  19.                         <div class="news_text">
  20.                             <a class="cat"><?php the_time('F j, Y'); ?></a>
  21.                             <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
  22.                             <p><?php the_excerpt(); ?></p>
  23.                         </div>
  24.                     </div>                            
  25.                 </div> <!-- end of single -->
  26.         <?php endwhile; ?>
  27. 2. To style css, put the code in style.css:
  28. .pagination {
  29.     clear:both;
  30.     position:relative;
  31.     font-size:11px; /* Pagination text size */
  32.     line-height:13px;
  33.     float:right; /* Pagination float direction */
  34. }
  35.  
  36. .pagination span, .pagination a {
  37.     display:block;
  38.     float:left;
  39.     margin: 2px 2px 2px 0;
  40.     padding:6px 9px 5px 9px;
  41.     text-decoration:none;
  42.     width:auto;
  43.     color:#fff; /* Pagination text color */
  44.     background: #555; /* Pagination non-active background color */
  45.     -webkit-transition: background .15s ease-in-out;
  46.     -moz-transition: background .15s ease-in-out;
  47.     -ms-transition: background .15s ease-in-out;
  48.     -o-transition: background .15s ease-in-out;
  49.     transition: background .15s ease-in-out;
  50. }
  51.  
  52. .pagination a:hover{
  53.     color:#fff;
  54.     background: #6AAC70; /* Pagination background on hover */
  55. }
  56.  
  57. .pagination .current{
  58.     padding:6px 9px 5px 9px;
  59.     background: #6AAC70; /* Current page background */
  60.     color:#fff;
  61. }
  62.  
  63. 3. Put the code in functions.php [NO CHANGES NEEDED]
  64. // numbered pagination
  65. function pagination($pages = '', $range = 4)
  66. {  
  67.      $showitems = ($range * 2)+1;  
  68.  
  69.      global $paged;
  70.      if(empty($paged)) $paged = 1;
  71.  
  72.      if($pages == '')
  73.      {
  74.          global $wp_query;
  75.          $pages = $wp_query->max_num_pages;
  76.          if(!$pages)
  77.          {
  78.              $pages = 1;
  79.          }
  80.      }  
  81.  
  82.      if(1 != $pages)
  83.      {
  84.          echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
  85.          if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
  86.          if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
  87.  
  88.          for ($i=1; $i <= $pages; $i++)
  89.          {
  90.              if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
  91.              {
  92.                  echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
  93.              }
  94.          }
  95.  
  96.          if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";  
  97.          if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
  98.          echo "</div>\n";
  99.      }
  100. }
  101.  
  102. 4. CALL THE PAGINATION FUNCTION IN YOUR TEMPLATE PAGE WITHIN PAGINATION DIV:
  103.  <?php if (function_exists("pagination")) {
  104.     pagination($custom_query->max_num_pages);
  105.  } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement