有一个问题与我的非常相似,可以找到here. 我的问题的不同之处在于:我的搜索表单有好几页(page.php
和single.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.phpif(is_page(\'overview\')) {
get_template_part(\'templates/overview\', \'main\');
}
在上
single.php (包括相同的文件)
if(is_singular(\'cities\')) {
get_template_part(\'templates/overview\', \'main\');
}
在上
overview-main.phpinclude_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”参数并将其传递到查询中