【问题标题】:Pagination and custom WP Query分页和自定义 WP 查询
【发布时间】:2017-01-19 14:49:25
【问题描述】:

我允许我的访问者通过我的 category.php 上的价格过滤器来订购帖子。他们有 2 个选项:“最高价格”和“最低价格”。选择过滤器后,查询工作正常。出现分页,但无法正常工作。它总是在我的“page/2/”、“page/3/”上显示第一个帖子...

我不知道为什么它不起作用你能帮帮我吗?

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

【问题讨论】:

  • 您不应该将 $paged 传递给您的 $args 吗?
  • @BlazejChecinski 是的,这是工作!

标签: php wordpress pagination advanced-custom-fields


【解决方案1】:

解决方案只有 $args 中的这个!

'paged'          => $paged,

【讨论】:

    猜你喜欢
    • 2017-04-18
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多