dsljpwi494719 2017-01-19 14:49
浏览 60

分页和自定义WP_Query

I allow my visitors to order posts by a price filter on my category.php. They have 2 options: "Max Price" and "Min Price". When a filter is selected the query works fine. Pagination appears but it doesn't work correctly. It always shows the first post on my "page/2/", "page/3/"...

I don't know why it doesn't work Can you help me?

    <?php get_header(); ?>
    <section id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <header class="pages-header col-md-12">
                <h1 class="page-title"> <?php single_cat_title(); ?> </h1>

                    <?php echo get_field('petite_description_cat', 'category_' . $cat);?>
                </header><!-- .page-header -->

                  <?php
                        //Display form for the query
                     if($_GET['minprice'] && !empty($_GET['minprice']))
                        {
                            $minprice = $_GET['minprice'];
                        } else {
                            $minprice = 0;
                        }

                        if($_GET['maxprice'] && !empty($_GET['maxprice']))
                        {
                            $maxprice = $_GET['maxprice'];
                        } else {
                            $maxprice = 999999;
                        }
                    ?>

                <form method="get">
                    <label>min:</label>
                      <input type="number" name="minprice" value="<?php echo $minprice; ?>">
                    <label>max:</label>
                      <input type="number" name="maxprice" value="<?php echo $maxprice; ?>">
                    <button type="submit" name="">Filter</button>
                </form>

<?php
  // set up or arguments for our custom query
  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
      $args = array(
                'cat' => $cat,
                'post_type' => 'post',
                'posts_per_page' => 5,
                'meta_query' => array(
                      array(
                         'key' => 'prix',
                         'type' => 'NUMERIC',
                         'value' => array($minprice, $maxprice),
                         'compare' => 'BETWEEN'
                             ),
                     )
                 );
  // create a new instance of WP_Query
  $the_query = new WP_Query($args);
?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>

  <?php
    //on récupère le template
     get_template_part( 'content-category', get_post_format() ); ?>


<?php endwhile; ?>

<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
  <div class="clearfix"></div>

<?php //pagination
bootstrap_pagination();?>

<?php } ?>

<?php else: ?>
        <?php get_template_part( 'no-results', 'archive' ); ?>
  <?php endif; ?>

<?php
 wp_reset_query();
 //display description only on page 1
  $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
  if ($page ==1)  {  echo category_description ( get_category_by_slug ( 'category-slug' )-> term_id );
  } ?>

  </main><!-- #main -->
</section><!-- #primary -->

<?php get_footer(); ?>
  • 写回答

1条回答 默认 最新

  • douxiuyi6529 2017-01-19 16:43
    关注

    Solution is only this in $args !

    'paged'          => $paged,
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)