0
我有一個搜索表單。選擇一種分類法,並選擇一個或多個術語。我想分頁結果。分頁失敗,CPT和動態稅收查詢
下面是代碼:
<?php
if(isset($_POST['tax_type'])) {
$tax_type=$_POST['tax_type'];
$terms = $_POST[$tax_type];
$term_list = implode("','", $terms);
$term_list1 = implode(", ", $terms);
$term_list = "'".$term_list."'";
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$giftfinder_args = array(
'post_type' => 'products',
'posts_per_page'=>"1",
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => $tax_type,
'field' => 'slug',
'terms' => explode(',',$term_list),
),
),
);
echo '<div class="results"><span class="eyebrow">search results</span>';
echo '<div class="cat_label">“' . $term_list1 . '”</div></div>';
// the query
$giftfinder_query = new WP_Query($giftfinder_args);
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $giftfinder_query;
?>
<?php if ($giftfinder_query->have_posts()) : ?>
<!-- the loop -->
<?php while ($giftfinder_query->have_posts()) : $giftfinder_query->the_post();
$thumb = get_the_post_thumbnail(null, 'medium');
?>
<div class="prod"><a href="<?php the_permalink(); ?>"><?php if(empty($thumb)) {echo '<div style="width:265px;height:265px;background:#eee;"></div>';} else {the_post_thumbnail('medium');} ?></a><span class="truncate"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span></div>
<?php endwhile; ?>
<div style="clear:both;"></div>
<!-- end of the loop -->
<?php
the_posts_pagination(array(
'prev_text' => __('Previous page', 'twentyfifteen'),
'next_text' => __('Next page', 'twentyfifteen'),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __('Page', 'twentyfifteen') . ' </span>',
));
?>
<?php wp_reset_postdata(); ?>
<?php else : get_template_part('content', 'none'); endif; ?>
<?php } ?>
第一頁是好的,但沒有結果出現在分頁頁面。
正如你所看到的,我試圖使用分頁修復,輪流我查詢到$ wp_query:
// the query
$the_query = new WP_Query($args);
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
但就像我說的,超出了第一頁沒有結果。
我打算嘗試pre_get_posts,但我無法弄清楚如何編寫代碼,因爲每個查詢都有基於用戶選擇的分類和搜索術語的唯一參數。
我也嘗試'add_args'paginate_links()函數,但無法弄清楚如何格式化得到的獲取字符串。
順便說一句,現在,查詢只是返回一個帖子,以便於檢查分頁。
任何建議表示讚賞!
感謝史蒂夫!我固定$ paged,但仍然沒有去。至少錯字不見了! – webguy