• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

how to display next previous pagination on custom page?

saranganime

Member
Dec 23, 2019
34
8
8
I tried to add pagination my custom page, the pagination number appears but there is no next and previous pagination.

VyOll.png

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)."'>&laquo; First</a>";
        if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; 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";
    }
}
 
I tried to add pagination my custom page, the pagination number appears but there is no next and previous pagination.

VyOll.png

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)."'>&laquo; First</a>";
        if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; 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";
    }
}
because this $showitems = ($range * 2)+1; it will default for 9 paged and you only have 4 paged, if you want to show the next and the prev button
PHP:
//first change this code
'posts_per_page' => 20, // reduce this number for example to 8

//after that find this code
B. function pagination($pages = '', $range = 4)
   //change to
   function pagination($pages = '', $range = 1) //1 or 2. i think 2 is better but for test just put 1
just it no need plugin
 
Last edited:
  • Like
Reactions: saranganime
because this $showitems = ($range * 2)+1; it will default for 9 paged and you only have 4 paged, if you want to show the next and the prev button
PHP:
//first change this code
'posts_per_page' => 20, // reduce this number for example to 8

//after that find this code
B. function pagination($pages = '', $range = 4)
   //change to
   function pagination($pages = '', $range = 1) //1 or 2. i think 2 is better but for test just put 1
just it no need plugin

this work, so many thanks for helping me
 
  • Like
Reactions: smalok
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock