2014-05-25 32 views
2

我有一個wp查詢循環。我想檢查帖子是否屬於某些類別。我可以使用the_category()獲得該類別。我曾嘗試檢查wp循環中的類別

if(the_category()==`car`){do somthing} 

,以及如何推動除轎車品類中所有剩餘的帖子後,所有的「汽車」類別。

+0

你需要首先顯示來自'car'類別的職位,然後將所有其他職位不屬於'car'類別? –

+0

@PieterGoosen yes.exactly –

回答

1

the_category()返回很多類別。

你可能想嘗試get the category

$categories = get_the_category(); 
foreach($categories as $cat) { 
    if($cat->cat_name == 'car') { 
    // do something 
    } 
} 
+0

以及如何將除汽車類別之外的所有剩餘帖子推到所有'汽車'類別之後。看到更新的問題 –

+0

我不能迭代通過循環。當嘗試回聲gettype(the_category());它顯示NULL.but回聲the_category()有輸出。 –

1

你可以在這裏運行兩個查詢。第一個查詢獲取汽車護理的所有帖子。第二個查詢獲取除car類別的帖子之外的所有其他帖子。只需記住用汽車類別的ID更改CATID FOR CAR,並且不要忘記第二個查詢中的ID前的減號。減號表示排除。

可以在食品閱讀更多內容:WP_Query

$do_not_duplicate = array(); 

$args = array(
    'cat' => CATID FOR CAR 
); 

$carargs = new WP_Query($args); 

if($carargs->have_posts()): 

    while ($carargs->have_posts()) : $carargs- >the_post(); 
    $do_not_duplicate[] = $post->ID; 

<----your loop----> 

    endwhile; 

endif; 

wp_reset_postdata(); 


$args2 = array(
    'cat' => -CATID FOR CAR, 
    'post__not_in' => $do_not_duplicate 
); 

$restargs = new WP_Query($args2); 

if($restargs->have_posts()): 

    while ($restargs->have_posts()) : $restargs- >the_post(); 
    $do_not_duplicate[] = $post->ID; 

<----your loop----> 

    endwhile; 

endif; 

wp_reset_postdata(); 
+0

我已經這樣做了。但分頁問題。我不能使用分頁,如果有多個循環 –

+0

看到我的這個問題也http://windows.stackexchange.com/questions/145556/how-to-show-the-posts-of-some-category -first-and-then-all-other –

+0

你需要爲這兩個查詢分頁,還是隻是第二個查詢? –