2013-02-05 63 views
0

我有以下查詢,它旨在顯示分類中的術語列表,並在每個術語下顯示分配給該術語的帖子列表。WordPress獲取條款和帖子查詢正在打破其他帖子

這會出現在我的single.php文章頁面左側的側邊欄中。在頁面的主要區域中,實際的單個帖子是爲了顯示。

但是,不是顯示當前的單個帖子,而是僅顯示最近單個帖子的TITLE。

這裏是我的查詢:

$terms = get_terms('benefit-cats'); 
    echo "<ul>"; 
    foreach ($terms as $term) { 
     $wpq = array ('taxonomy'=>'benefit-cats','term'=>$term->slug); 
     $query = new WP_Query ($wpq); 
     echo "<li class=\"list-item\">".$term->name.""; //<a href=\"".get_term_link($term->slug, 'benefit-cats')."\"></a>// 
     echo "<ul class=\"children\">"; 
     $posts = $query->posts; 
     foreach ($posts as $post) { 
      echo "<li><a href=\"".get_permalink()."\">".$post->post_title."</a></li>"; 
     }  
     echo "</ul></li>"; 
    } 
    echo "</ul>"; 

我曾嘗試加入重置查詢到的代碼,但沒有成功。我發現這是導致問題的這個特定部分:

$posts = $query->posts; 
      foreach ($posts as $post) { 
       echo "<li><a href=\"".get_permalink()."\">".$post->post_title."</a></li>"; 
      } 

我究竟在做什麼錯在這裏?我一直在解決這個問題30-40分鐘,但沒有取得任何成功。

希望能解釋我的錯誤。

回答

2

嘗試使用

<?php 
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> 
echo "<li><a href=\"".get_permalink()."\">".the_title()."</a></li>"; 
<?php endwhile; endif; wp_reset_query(); ?> 

代替

$posts = $query->posts; 
      foreach ($posts as $post) { 
       echo "<li><a href=\"".get_permalink()."\">".$post->post_title."</a></li>"; 
      } 

希望這將罰款你

0

你的論點WP_Query沒有很好形成。

下面是它應該如何分類學(從Codex拍攝片段):

$args = array(
    'post_type' => 'post', 
    'tax_query' => array(
     array(
      'taxonomy' => 'people', 
      'field' => 'slug', 
      'terms' => 'bob' 
     ) 
    ) 
); 

後你會得到你的論點正確,你應該loop在結果:

while($query->have_posts()): 
    $query->the_post(); 
    // It is now that get_permalink() will work 
endwhile; 
相關問題