Enter your keyword

Blog

WordPress wp_query With Custom Pagiantion

<?php
//The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
        'post_type' => 'post',
        'paged' => $paged,
        'order' => 'DESC',
        'posts_per_page' => '5'
);
$query = new WP_Query($args);
 
if ($query->have_posts())
while ($query->have_posts()) : $query->the_post();
        echo '<h1>' . get_the_title() . '</h1>';
        the_excerpt();
endwhile; // end of the loop.
 
/* Pagination  Code Start  */
global $wp_query;
$big = 999999999; // need an unlikely integer
$totalpages = $query->max_num_pages;
$current = max(1, get_query_var('paged'));
$paginate_args = array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))) ,
        'format' => '?paged=%#%',
        'current' => $current,
        'total' => $totalpages,
        'show_all' => False,
        'end_size' => 1,
        'mid_size' => 3,
        'prev_next' => True,
        'prev_text' => __('« Previous') ,
        'next_text' => __('Next »') ,
        'type' => 'plain',
        'add_args' => False,
        'add_fragment' => '',
        'before_page_number' => '',
        'after_page_number' => ''
);
$pagination= paginate_links($paginate_args);
echo "<nav class='pagination'>";
echo "<span class='page-numbers page-no'>Page " . $paged . " of " . $totalpages . "</span> ";
echo $pagination;
echo "</nav>";
 
/* Pagination  Code End  */

[/spb_text_block] [spb_text_block pb_margin_bottom="no" pb_border_bottom="no" width="1/1" el_position="first last"]

/* ADD This CSS For Set Design Of Pagination */
.pagination span,
.pagination a {
  display: inline-block;
  padding: 2px 10px;
}
.pagination a {
    background-color: #000;
    color: #fff;
    text-decoration: none;
}
.pagination a:hover {
  background-color: #ff3c50;
  color: #fff;
}
.pagination span.page-no {
  margin-right: 10px;
  padding: 0;
}
.pagination span.dots {
  padding: 0;
  color: black;
}
.pagination span.current {
  background-color: #bb1e32;
  color: #fff;
}

No Comments

Add your review

Your email address will not be published.

* Feilds are Required.