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>

1 Answer
1

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
        ) );

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *