Add Pagination in custom loop

时间:2017-04-10 作者:Konstantinos Koletsas

我正在尝试向自定义WPQuery循环添加分页。

我几乎尝试了书中的每一个技巧,但都没有成功。

有什么想法吗?提前谢谢你。

<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 个回复
SO网友:Aamer Shahzad

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(); ?>
SO网友:Aniruddha Gawade

据我所知,您使用的函数使用全局$wp_query. 因此$fp_query 尝试:

global $wp_query;
$args = array(); //your args
$wp_query = new WP_Query($args);
//your loop
next_posts_link();
previous_posts_link();
wp_reset_query();
还请注意wp_reset_query() 在零件分页之后。

相关推荐

Count posts for pagination

我正在为一个网站分页<;上一页(页码)下一页>很简单,已经完成。但是现在我需要添加一个选择器来直接转到页面(例如:转到第7页),要这样做,我需要知道有多少页面,为此我需要计算在查询中找到了多少帖子。问题是这个网站有太多的帖子(>13.000),查询所有帖子都会减慢页面加载速度,这就像。。。10秒后页面才能加载。显然,这是不可接受的。分页解决了这个问题,因为一次只加载50或100篇文章,但我无法将它们全部计算在内。我可以在不加载的情况下统计某个查询中的帖子吗?或者我可以通过其他方式获得页数吗