2

My pagination is not working on front page for custom post type, its working properly when is not select for front page.

My code in functions.php

/** pagination **/
function fruit_pagination($pages = '', $range = 5)
{  
 $fruit_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;
     }
 } echo '
 <div class="site-pagination">';  
 if(1 != $pages)
 {

 echo "
 <ul>
 ";
  if($paged > 2 && $paged > $range+1 && $fruit_showitems < $pages) echo "
 <li class='pagination-previous-all'>
 <a href='".get_pagenum_link(1)."'>
 <span class='sprite previous-all-icon'>First</span></a></li>
  ";
 if($paged > 1 && $fruit_showitems < $pages) echo "
 <li class='pagination-previous'>
  <a href='".get_pagenum_link($paged - 1)."'>
  <span class='sprite previous-icon'>Previous</span></a></li>
   ";
  for ($i=1; $i <= $pages; $i++)
  {
 if (1 != $pages &&( !($i >= $paged+$range+1 || 
 $i <= $paged-$range-1)      || $pages <= $fruit_showitems ))
 {
 echo ($paged == $i)? "
 <li class='active'><a href='#' >".$i."</a></li>
 ":"
 <li><a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a></li>
 ";
  }
 }
 if ($paged < $pages && $fruit_showitems < $pages) echo "
 <li class='pagination-next'>
 <a href='".get_pagenum_link($paged + 1)."'>
  <span class='sprite next-icon'>Next</span></a></li>
   ";  
  if ($paged < $pages-1 &&  $paged+$range-1 < $pages && 
  $fruit_showitems < $pages) echo "
 <li class='pagination-next-all'>
 <a href='".get_pagenum_link($pages)."'>
 <span class='sprite next-all-icon'>Last</span></a></li>
 ";
 echo "
  </ul>
   \n";
  }
  echo '</div>
  ';
  }
  /** End pagination **/ 

My code in Custom Page Template and it is selected for Front Page

   global $paged;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    $rarthemes_html_args = array(
                        'paged' => $paged,
                        'orderby' => 'post_date',
                        'order' => 'DESC',
                        'post_type' => 'html',
                        'post_status' => 'publish',
                    );
                    $rarthemes_html = new WP_Query($rarthemes_html_args);
                    while ($rarthemes_html->have_posts())
          { $rarthemes_html->the_post(); the_title(); }

      <div class="col-md-12">
        <?php
                if (function_exists('fruit_pagination')) {

               fruit_pagination($rarthemes_html->max_num_pages, '2');
                }
                ?>
      </div>
5
  • 2
    Static front pages uses get_query_var( 'page' ) and not get_query_var( 'paged' ). Please consider using the site search as there are tons on info on this same exact issue – Pieter Goosen Jan 12 '16 at 5:24
  • Here is function that covers all basis and works out of the box for any query – Pieter Goosen Jan 12 '16 at 5:26
  • @PieterGoosen For some reason (from the last WP update I think), get_query_var( 'page' ) is not working anymore, it just returns empty string. – Matija Mrkaic Jan 12 '16 at 10:34
  • Update: It seems that v4.4.1 has a bug with get_query_var('page'). Can be tracked here: core.trac.wordpress.org/ticket/35344 – Matija Mrkaic Jan 12 '16 at 10:51
  • Thanks for the bug report. Did not know about this. – Pieter Goosen Jan 12 '16 at 11:35
-3

this is proper working code for pagination on forntpage

if ( get_query_var('paged') ) {

            $paged = get_query_var('paged');

        } elseif ( get_query_var('page') ) {

            $paged = get_query_var('page');

        } else {

            $paged = 1;

        }

        $args = array( 
                  'post_type' => 'post', 
                  'posts_per_page' => 8, 
                  'post_status'   => 'publish',
                  'paged'          => $paged
         );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
         the_title(); the_content();
        endwhile;

and pagination code here

$big = 999999999; // need an unlikely integer

        echo paginate_links( array(
          'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
          'format' => '?paged=%#%',
          'current' => max( 1, get_query_var('page') ),
          'total' => $loop->max_num_pages
        ) );
1
  • 1
    no one should ever use query_posts for whatever reason, especially not in a context in which pagination is needed. – Mark Kaplun Feb 6 '18 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.