Return to Snippet

Revision: 40725
at February 6, 2011 05:38 by brewern


Initial Code
<?php
$blog = new WP_Query();
$args = array(
    'posts_per_page' => 5,
    'paged' => $paged
);
$blog->query($args);
if($blog->have_posts()):
    while ($blog->have_posts()):
        $blog->the_post(); // Sets up the post
?>
<h2><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
<?php the_excerpt() ?>
<?php
    endwhile;
/* -----------------------
    FUNCTION IS BELOW
----------------------- */
wp_pagination_nav($blog, $paged);
wp_reset_query();
endif;
?>
<?php
/* --------------------------------------
  ADD PAGINATION FUNCTION TO YOUR
  FUNCTIONS.PHP FILE
-------------------------------------- */
function wp_pagination_nav($loop, $paged){
  if($loop->max_num_pages>1):
      echo '<div class="pagination">';
      if ($paged > 1):
        echo '<a href="?paged='.($paged-1).'"><</a> ';
      endif;
      
      for($i=1;$i<=$loop->max_num_pages;$i++){
        if($paged==$i || ($i==1 && $paged == 0)){
          $class = "selected";
        } else {
          $class = '';
        }
        echo '<a href="?paged='.$i.'" class="'.$class.'">'.$i.'</a> ';
      }
      
      if($paged < $loop->max_num_pages):
        $nextpage = $paged+1;
        if($paged == 0) $nextpage = 2;
        echo '<a href="?paged='.$nextpage.'">></a> ';
      endif;
      echo '<div class="clear"></div></div>';
  endif;
}
?>

Initial URL

                                

Initial Description

                                

Initial Title
Wordpress custom loop with pagination

Initial Tags
wordpress

Initial Language
PHP