2014-05-07 24 views
-1

根據類別ID,我試圖檢索該類別下的相關產品。不過,我嘗試了所有類別ID彷彿回到誰屬於該類別ID的所有產品我在數據庫中,而不是這些,這是我的相關代碼:我的代碼總是返回指定類別的所有產品ID

$args = array(
      'post_type' => 'product', 
      'posts_per_page' => -1, 
      'category' => 24 
     ); 

$loop = new WP_Query($args); 


while ($loop->have_posts()) : $loop->the_post(); 

echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.the_title().'</a>'; 

endwhile; 
wp_reset_query(); 

即使我ommit類的說法,代碼仍然返回所有產品。如何解決這個問題,我只能得到指定類別ID的相關產品?

在此先感謝

回答

1

基於阿南德的意見,我想通了,這樣做的方式:

$term = get_term($_GET['catId'],'product_cat');// first parameter is the cat ID 

$args = array('post_type' => 'product', 
       'posts_per_page' => -1,// unlimited posts 
       'product_cat' => $term->name,// Pass the category name 
     ); 

此處瞭解詳情:http://codex.wordpress.org/Function_Reference/get_term

+0

這不是關於upvote,試圖看看我沒有幫助你:) –

1
$term = get_term(24, 'product_cat'); 

$args = array(
      'post_type' => 'product', 
      'posts_per_page' => -1, 
      'product_cat' => $term->name 
     ); 
+0

'cat'不以工作所有 – Malloc

+0

我的道歉,沒有意識到它是Woocommerce產品,您需要使用'product_cat'和類別的名稱。上面更新的代碼。 –

+0

謝謝,但我處理類別ID不是類別名稱:) – Malloc

相關問題