-1

I am trying to add pagination to my custom WPQuery loop.

I tried pretty much every trick in the book but it does not work.

Any ideas on that ? Thank you in advance.

<div class="container"<?php if($background_color != "") { echo " style='background-color:". $background_color ."'";} ?>>
    <?php if(isset($qode_options_proya['overlapping_content']) && $qode_options_proya['overlapping_content'] == 'yes') {?>
        <div class="overlapping_content"><div class="overlapping_content_inner">
    <?php } ?>
    <div class="container_inner default_template_holder clearfix frontpage">
        <div class="container">
            <?php
                $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
              $args=array(
                 'post_type' => 'gadget',
                 'post_status' => 'publish',
                 'posts_per_page' => 24,
                 'paged' => $paged
                );

              $fp_query = null;
              $fp_query = new WP_Query($args);

              if( $fp_query->have_posts() ) {

                $i = 0;
                while ($fp_query->have_posts()) : $fp_query->the_post();
              // modified to work with 3 columns
              // output an open <div>
              if($i % 3 == 0) { ?> 

              <div class="vc_row prd-box-row">

              <?php
              }
              ?>
                <div class="vc_col-md-4 vc_col-sm-6 vc_col-xs-12 prd-box row-height">
                    <div class="prd-box-inner">
                    <div class="prd-box-image-container">
                      <a href="<?php the_permalink(); ?>">
                        <div class="prd-box-image" style ="background-image: url(
                            <?php /* ACF */ the_field('header_image'); ?>
                            <?php /* Types */ $headerimage = get_post_meta(get_the_ID(), 'wpcf-headerimage'); 
                                var_dump($headerimage);
                                echo types_render_field('wpcf-headerimage', array("raw"=>"true", "url"=>"true"));
                            ?>);">
                          <div class="vc_col-xs-12 prd-box-date"><?php $short_date = get_the_date( 'M j' ); echo $short_date;?><span class="full-date">, <?php $full_date = get_the_date( 'Y' ); echo $full_date;?></span></div>
                          <div class="badges-container">
                            <!-- Discount Condition -->
                            <div class="vc_col-xs-2 discounted-badge">
                                <i class="fa fa-percent" aria-hidden="true"></i>
                            </div>
                            <!-- Video Condition -->
                            <div class="vc_col-xs-2 video-badge">
                                <i class="fa fa-play" aria-hidden="true"></i>
                            </div>
                            <!-- Crowdfunding Condition -->
                            <div class="vc_col-xs-2 crowdfunding-badge">
                                <i class="fa fa-money" aria-hidden="true"></i>
                            </div>
                          </div>
                        </div>
                      </a>
                    </div>
                    <div class="prd-separator"></div>
                    <div class="vc_row prd-box-info">
                        <div class="vc_col-xs-12 prd-box-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
                    </div>
                    <div class="prd-separator"></div>
                    <div class="vc_row">
                      <div class="vc_col-xs-6">
                        <div class="vc_col-xs-12 prd-box-price">$10</div>
                      </div>
                      <div class="vc_col-xs-6 prd-box-category">
                        <?php
                          $term_slug     = get_query_var('term');
                          $taxonomy_name = get_query_var('taxonomy');
                          $current_term  = get_term_by('slug', $term_slug, $taxonomy_name);
                          if ( true === is_a( $current_term, 'WP_Term' ) ) {
                              $args = array(
                                  'child_of' => $current_term->term_id,
                                  'orderby'  => 'id',
                                  'order'    => 'DESC'
                              );
                              $terms = get_terms($taxonomyName, $args);
                              if ( true === is_array( $terms ) ) {
                                  foreach ($terms as $term) {
                                      echo '<li id="mid"><a href="#tab-' . esc_attr( $term->slug ) . '">' . esc_html( $term->name ) . '</a></li>';
                                  }
                              }
                          }
                        ?>
                      </div>
                    </div>
                    <div class="vc_row prd-box-share-wh-row">
                        <div class="prd-separator"></div>
                        <div class="prd-box-share-container">
                          <div class="vc_col-xs-1"></div>
                            <div class="vc_col-xs-4"><?php echo do_shortcode( '[addtoany]' );?></div>
                          <div class="vc_col-xs-1"></div>
                        </div>
                        <div class="prd-box-wishlist-container">
                          <div class="vc_col-xs-1"></div>
                            <div class="vc_col-xs-4">
                                <?php $arg = array ( 'echo' => true ); do_action('gd_mylist_btn',$arg); ?>
                            </div>
                          <div class="vc_col-xs-1"></div>
                        </div>
                    </div>
                    <div class="prd-separator"></div>
                    </div>
                </div>  

                <?php $i++; 
                 // Closing the grid row div   
                if($i != 0 && $i % 3 == 0) { ?>
                    </div><!--/.row-->
                    <div class="clearfix"></div>
                  <?php } ?>
                  <!-- Random Category Snippet Generation -->
                  <?php 
                        if( $i % 12 == 0 ) {
                            $max = 1; //number of categories to display
                            $taxonomy = 'gadget_categories';
                            $terms = get_terms($taxonomy, 'orderby=name&order= ASC&hide_empty=0');

                            // Random order
                            shuffle($terms);

                            // Get first $max items
                            $terms = array_slice($terms, 0, $max);

                            // Sort by name
                            usort($terms, function($a, $b){
                            return strcasecmp($a->name, $b->name);
                            });

                            // Echo random terms sorted alphabetically
                            if ($terms) {
                            foreach($terms as $term) {
                                $termID = $term->term_id;
                                echo '<div class="random-category-snippet vc_row" style="background-image: url('.types_render_termmeta("category-image", array( "term_id" => $termID, "output" =>"raw") ).')"><div class="random-snippet-inner"><a href="' .get_term_link( $term, $taxonomy ) . '" title="' .  sprintf( __( "View all gadgets in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a><p>' . $term->description . '</p><br><br><a class="explore-category" href="'. esc_url( get_term_link( $term ) ) .'">Explore This Category</a></div></div> ';
                            }
                            }
                       }
                  ?>

                <?php  
                    endwhile;
                }
                    wp_reset_query();
                ?>
                <!-- pagination -->
                <?php next_posts_link(); ?>
                <?php previous_posts_link(); ?>
        </div><!--/.container-->  
    </div>      

2 Answers2

-1

source : How to fix pagination for custom loops?

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
   'post_type' => 'gadget',
   'post_status' => 'publish',
   'posts_per_page' => 24,
   'paged' => $paged
);

$temp = $wp_query;
$fp_query = null;
$fp_query = new WP_Query($args);

if ( $fp_query->have_posts() ) : while ( $fp_query->have_posts() ) : $fp_query->the_post();   

/* DO STUFF IN THE LOOP */

endwhile; endif;

next_posts_link();
previous_posts_link();

$fp_query = null;
$fp_query = $temp;
wp_reset_query(); ?>
Aamer Shahzad
  • 1,673
  • 17
  • 18
-2

As far as I remember, the functions you are using uses global $wp_query. So instead of $fp_query try:

global $wp_query;
$args = array(); //your args
$wp_query = new WP_Query($args);
//your loop
next_posts_link();
previous_posts_link();
wp_reset_query();

Also notice that wp_reset_query() is after pagination parts.

Aniruddha Gawade
  • 1,213
  • 1
  • 10
  • 12