Skip to content

Instantly share code, notes, and snippets.

@jjjjcccjjf
Last active September 24, 2020 03:19
Show Gist options
  • Save jjjjcccjjf/56262f553b7cc40910f07bcb864dc437 to your computer and use it in GitHub Desktop.
Save jjjjcccjjf/56262f553b7cc40910f07bcb864dc437 to your computer and use it in GitHub Desktop.
Wordpress pagination code snippet
############################# function-pagination.php ##########################
<?php
/**
* @link: http://callmenick.com/post/custom-wordpress-loop-with-pagination
* Create this as a separate file function-pagination.php
*/
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It's good because we can now override default pagination
* in our theme, and use this function in default quries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our 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' => __('&laquo;'),
'next_text' => __('&raquo;'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<div class='pagination'>";
// echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</div>";
}
}
?>
############################# / function-pagination.php ##########################
################################## example usage ################################
# Include function-pagination.php in the seperate file
<?php
/**
* @usage
*/
/**
* include the custom pagination here
*/
include_once('function-pagination.php');
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
# QUERY ARGS HERE
$args = array(
'post_type' => 'blog',
'posts_per_page' => 1,
'paged' => $paged
);
$loop = new WP_Query( $args );
if ($loop->have_posts()) : while ( $loop->have_posts() ) : $loop->the_post();
?>
<li>
<div class="featured-img"><img src="<?php the_post_thumbnail_url(); ?>"></div>
<div class="details">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<h5>Date Posted: <?php echo date("F d, Y", strtotime(get_the_date())); ?></h5>
<p><?php echo substr(get_the_excerpt(), 0, 100) . " ..."; ?></p>
<p><a href="<?php the_permalink(); ?>">Read More</a></p>
</div>
</li>
<?php
endwhile;
/* Pagination */
if (function_exists(custom_pagination)) {
custom_pagination($loop->max_num_pages,"",$paged);
}
# Make sure to change $loop variable to your actual loop variable!!!!
/* Pagination */
wp_reset_postdata();
?>
################################## / example usage ################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment