自定义查询表单提交分页

时间:2013-11-08 作者:eskimo

有一个问题与我的非常相似,可以找到here. 我的问题的不同之处在于:我的搜索表单有好几页(page.phpsingle.php) 我不使用wp pagenavi。我尝试了张贴在那里的建议解决方案,但都不起作用。

What is working

  • 查询显示正确的结果提交表单后,查询显示正确的结果,并显示正确的链接(例如:com/overview/?paged=2和example.com/city/boston/?paged=2)
  • 使用相同设置的分页可用于搜索。php
What is not working: 如果我提交表单并单击除第1页以外的任何页面,URL就会成为示例。com/概述/?paged=2或示例。com/城市/波士顿/?paged=2,但不显示结果。

Updated after the accepted answer:请阅读代码中的注释以了解问题是如何解决的。

--- Below here the setup of my code

在…上page.php

if(is_page(\'overview\')) {
    get_template_part(\'templates/overview\', \'main\');
}
在上single.php (包括相同的文件)

if(is_singular(\'cities\')) {
    get_template_part(\'templates/overview\', \'main\');
}
在上overview-main.php

include_once \'overview-search-input.php\'; 
include_once \'overview-sidebar.php\';
在上overview-search-input.php (changed after UPDATE 3, original code below here)

   if(!empty($_GET[\'period\'])) {
        $period = $_GET[\'period\'];
   }
   else {
        $period = \'upcoming\'; // Set to upcoming if nothing is selected
   }

   $query = search_v1($period, $paged); // Passing the $paged parameter in the query

   $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; // The $paged parameter
在上overview-sidebar.php

<form action="<?php echo get_permalink(); ?>" method="get"> 
<select name="period" placeholder="Select">
  <option value=""></option>
  <option value="all"></option>
  <option value="upcoming"></option>
</select>
</form>
在函数中search_v1:

    $args = array(
        \'post_type\'         => \'events\',
        \'post_status\'       => \'publish\',
        \'posts_per_page\'    => 20,
        \'paged\'             => $paged, // This was passed into the function
        \'meta_query\'        => array($meta_query), // This is generated based on the $period input and it\'s working (tested)
        \'meta_key\'          => \'date_from\',
        \'orderby\'           => \'meta_value\',
        \'order\'             => \'ASC\',
    );

    return new WP_Query($args);
然后开始overview-main.php

   if($query->have_posts()): while ($query->have_posts()): $query->the_post();
      // Do stuff
    endwhile;

    create_pagination($query);

    wp_reset_postdata();
    endif;
在函数中create_pagination:

$query->query_vars[\'paged\'] > 1 ? $current = $query->query_vars[\'paged\'] : $current = 1;

$args = array(
    \'base\' => @add_query_arg(\'paged\',\'%#%\'),
    \'format\' => \'?paged=%#%\',
    \'total\' => $query->max_num_pages,
    \'current\' => $current,
    \'show_all\' => false,
    \'end_size\' => 1,
    \'mid_size\' => 2,
    \'prev_next\' => true,
    \'prev_text\' => \'<i class="icon-chevron-left"></i>\',
    \'next_text\' => \'<i class="icon-chevron-right"></i>\',
    \'type\' => \'list\'
);

return paginate_links($args);
The problem was: 由于这两个分页函数都可以工作并显示正确的分页链接,我认为这不是问题所在。我认为这取决于自定义查询(因为当结果生成一次而不被表单输入更改时,这种分页在search.php上有效)。

The problem was solved: 通过使用“paged”参数并将其传递到查询中

2 个回复
最合适的回答,由SO网友:gmazzap 整理而成

您的代码中有一些内容需要更改:

请不要使用$_SESSION 东西所以不要在任何地方添加session\\u start。您可以通过查询字符串传递当前状态,使用method="get" 以您的形式search_v1 函数作为参数,这样您就可以确保在必须访问时不会出现错误superglobals 建议使用数组,而不是直接访问filter_inputoverview-search-input.php 文件,最后一行是$query = search_v1($_SESSION[\'period\']); 这完全没有用,我认为在表单中很有用选择当前显示帖子的期间选项,例如,当您查看期间为“即将到来”的帖子时,应在表单中选择选项“即将到来”应用此更改:

  • page.phpsingle.php 并保持不变session_start(); 从您添加的位置

    overview-search-input.php

    <?php
    $period = filter_input(INPUT_GET, \'period\', FILTER_SANITIZE_STRING);
    $paged = get_query_var(\'paged\');
    if ( empty($period ) ) $period  = \'upcoming\';
    if ( empty($paged) ) $paged = 1;
    

    overview-sidebar.php

    <form action="<?php echo get_permalink(); ?>" method="get">
    <select name="period" placeholder="Select">
      <option <?php selected(\'\', $period) ?>></option>
      <option <?php selected(\'all\', $period) ?>>all</option>
      <option <?php selected(\'upcoming\', $period) ?>>upcoming</option>
    </select>
    </form>
    

    overview-main.php

    include_once \'overview-search-input.php\';
    include_once \'overview-sidebar.php\';
    
    $query = search_v1( $period, $paged );
    
    if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
      // Do stuff
    endwhile;
    
    echo create_pagination( $query, $period );
    
    wp_reset_postdata();
    endif;
    
    最后,这两项功能,search_v1create_pagination:

    function search_v1( $period = \'upcoming\', $paged = 1 ) {
      if ( empty($paged) ) $paged = 1;
      $meta_query = array(
        // create your meta query here
      );
      $args = array(
        \'post_type\'         => \'events\',
        \'post_status\'       => \'publish\',
        \'posts_per_page\'    => 20,
        \'paged\'             => $paged,
        \'meta_query\'        => array($meta_query),
        \'meta_key\'          => \'date_from\',
        \'orderby\'           => \'meta_value\',
        \'order\'             => \'ASC\',
      );
      return new WP_Query( $args );
    }
    
    
    function create_pagination( $query, $period = \'upcoming\' ) {
      $current = isset($query->query_vars[\'paged\']) ? $query->query_vars[\'paged\'] : 1;
      $args = array(
        \'base\' => get_permalink() . \'%_%\',
        \'format\' => \'?paged=%#%&period=\' . $period,
        \'total\' => $query->max_num_pages,
        \'current\' => ($current > 0) ? $current : 1,
        \'show_all\' => false,
        \'end_size\' => 1,
        \'mid_size\' => 2,
        \'prev_next\' => true,
        \'prev_text\' => \'<i class="icon-chevron-left"></i>\',
        \'next_text\' => \'<i class="icon-chevron-right"></i>\',
        \'type\' => \'list\'
      );
      return paginate_links($args);
    }
    

SO网友:Subharanjan

而不是这条线

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
你能用这个检查一下吗。

$paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
还有这个

$query->query_vars[\'paged\'] > 1 ? $current = $query->query_vars[\'paged\'] : $current = 1;

$query->query_vars[\'page\'] > 1 ? $current = $query->query_vars[\'page\'] : $current = 1;
Note: 由于您使用的是自定义页面模板,“paged”在那里不起作用。

结束

相关推荐

Pagination of RSS2 feed

如何对RSS2提要进行分页?我当前的RSS链接类似于:http://www.example.com/?feed=rss2 我试过:http://www.example.com/?feed=rss2&page=2 但是WordPress仍然返回相同的结果集,因为我在仪表板>设置>阅读>联合提要显示最新的(当然我有10多个帖子)