2017-08-07 77 views
1

我需要你的幫助。我有一個自定義搜索引擎來搜索產品後類型分類:Wordpress查詢與分頁

if(isset($_POST['search_products']) { 
    /// codes .... 
    $_SESSION['ids'] = $my_ids; 
    $args = array(
     'post_type' => 'product', 
     'showposts' => -1, 
     'post__in' => $_SESSION['ids'] 
    ) 
    $posts = new Wp_Query($args); 
} 

這個查詢輸出約60種產品分頁(每頁10個產品),但是當用戶訪問該頁面,而無需使用搜索引擎,所有應該顯示產品。相反,$_SESSION保留並只顯示以前的結果。

我只是希望分頁工作,當我做搜索時,以及當我不使用搜索引擎訪問頁面時顯示的所有產品。

是否有任何WordPress專家有一個想法?

謝謝。

+0

嘗試此鏈接:http://callmenick.com/post/custom-wordpress -loop-with-page –

回答

1
if(isset($_POST['search_products']) { 
    /// codes .... 
    $_SESSION['ids'] = $my_ids; 
    $args = array(
     'post_type' => 'product', 
     'showposts' => -1, 
     'post__in' => $_SESSION['ids'] 
    ) 
    $posts = new Wp_Query($args); 
} 
else { 

$args = array(
     'post_type' => 'product', 
     'showposts' => -1, 
    ) 
    $posts = new Wp_Query($args); 
} 

簡單地把一個else塊

+0

或者更乾淨: $ args = array( \t 'post_type'=> '產品', \t 'showposts'=> -1, ) 如果(isset($ _POST [ 'search_products'])){ $ _SESSION [ 'IDS'] = $ my_ids; $ args ['post__in'] = $ _SESSION ['ids']; } $ posts = new Wp_Query($ args); – windyjonas

+0

你好,謝謝你,但我必須發送表單$ _GET,它解決了這個問題。 – user44321

0

CODE,

global $post; 
$id = intval($_GET['cat']); 
$paged  = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$products = new WP_Query(array( 
           'post_type'  => 'products', 
           'order'   => 'ASC', 
           'posts_per_page' => 5, 
           'paged'   => $paged 
         )); 
endwhile; 

HTML,

<ul class="pagination pull-right"> 
<li><?php echo get_next_posts_link('Next Page', $products->max_num_pages); ?></li> 
<li><?php echo get_previous_posts_link('Previous Page'); ?></li> 
</ul>