2012-11-16 50 views
0

我已經創建了一個自定義帖子類型的常見問題和自定義分類組織問題&答案。WordPress的術語循環與foreach和WP_Qurey作爲嵌套循環問題

我也有創建以下單頁模板使用代碼來顯示我的常見問題

$terms = get_terms(
    'faq_categories', 
    array(
     'orderby' => 'name', 
     'order'  => 'ASC' 
    ) 
); 

foreach($terms as $term) 
{ 
    ?> 
    <h3><?php echo $term->name; ?></h3> 
    <?php 

    $q_args = array(
     'post_type'   => 'faq', 
     'tax_query'   => array(
      'taxonomy' => 'faq_categories', 
      'field'  => 'slug', 
      'terms'  => $term->slug 
     ), 
     'posts_per_page' => -1 
    ); 

    wp_reset_postdata(); 
    wp_reset_query(); 

    $ans = new WP_Query($q_args); 

    while($ans->have_posts()) 
    { 
     $ans->the_post(); 

     ?> 
     <h5><?php echo the_title(); ?></h5> 
     <?php 
    } 
} 

我的問題是,當我得到這個問題的標題,該問題未作回答問題分類並進行分組每個類別我都會重複提供所有可用的問題。

結果是這樣的:

Sale [FAQ Category] 
    How to buy? [FAQ Question] 
    What is the cost? [FAQ Question] 
    How can I contact you? [FAQ Question] 
    What is your address? [FAQ Question] 
Contacts [FAQ Category] 
    How to buy? [FAQ Question] 
    What is the cost? [FAQ Question] 
    How can I contact you? [FAQ Question] 
    What is your address? [FAQ Question] 

而且我已經嘗試wp_reset_postdate()wp_reset_query()前後WP_Query循環之後,我已經嘗試沒有運氣太刪除它們。

有關如何解決該問題的任何想法?

親切的問候 Merianos尼科斯

回答

1

tax_query接受數組的數組。

$q_args = array(
    'post_type'   => 'faq', 
    'tax_query'   => array(
     array(
      'taxonomy' => 'faq_categories', 
      'field'  => 'slug', 
      'terms'  => $term->slug 
     ) 
    ), 
    'posts_per_page' => -1 
); 

或者重寫查詢,你並不真的需要一個tax_query:

$ans = new WP_Query("post_type=faq&faq_categories=$term->slug&posts_per_page=-1");