saranganime
New member
- Dec 23, 2019
- 27
- 5
- 3
I tried to add pagination my custom page, the pagination number appears but there is no next and previous pagination.
popular.php
function.php

popular.php
PHP:
<?php
/*
Template Name: Popular List
*/
get_header(); ?>
<div id="content">
<div class="container">
<div class="page-header">
<h2 class="widget-title mt-4">Popular Videos</h2>
</div>
<div class="video-loop">
<div class="row no-gutters">
<div class="order-1 order-sm-1 order-md-1 order-lg-1 order-xl-1 col-12 col-md-6 col-lg-6 col-xl-6">
</div>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 20,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'paged' => $paged
);
$custom_query = new WP_Query( $args );
?>
<?php
while($custom_query->have_posts()) :
$custom_query->the_post();
?>
<div class="col-12 col-md-4 col-lg-3 col-xl-3">
<div class="video-block video-with-trailer">
<a class="thumb" href="<?php the_permalink(); ?>">
<div class="video-debounce-bar"></div>
<!-- <div class="lds-dual-ring"></div> -->
<?php
if (has_post_thumbnail())
{
the_post_thumbnail(array(429, 355, true, 'class' => 'video-img img-fluid', 'alt' => get_the_title()));
}
?>
<div class="video-preview"></div>
<span class="duration"><?php echo strip_tags ( get_the_term_list($post->ID, 'time') ); ?></span>
</a>
<a class="infos" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<span class="title"><?php the_title(); ?></span>
</a>
<div class="video-datas">
<span class="views-number"><?php echo getPostViews(get_the_ID()); ?></span>
<!-- <span class="rating"><i class="fa fa-thumbs-up"></i> 99%</span> -->
</div>
</div>
</div>
<?php endwhile; ?>
<nav aria-label="Posts navigation" class="d-none d-md-block col-12">
<?php if (function_exists("pagination")) {
pagination($custom_query->max_num_pages);
}
?>
</nav>
</div>
</div>
</div>
</div>
<?php
get_footer();
function.php
PHP:
function pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<ul class=\"pagination pagination-lg my-4 justify-content-center\">";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<li class=\"page-item active\"><a class=\"page-link\" href='".get_pagenum_link($i)."'>".$i."</a></li>":"<li><a href='".get_pagenum_link($i)."' class=\"page-link\">".$i."</a></li>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<li class=\"page-item\"><a href=\"".get_pagenum_link($paged + 1)."\" data-hover=\"Next\" class=\"next page-link\">»</a></li>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<li class=\"page-item\"><a href='".get_pagenum_link($pages)."' data-hover=\"Previous\" class=\"prev page-link\">«</a>";
echo "</ul>\n";
}
}