2013-02-20 33 views
0

我有一個類別查詢,我的類別查詢我想查詢的類別編號(或名稱或任何),以獲得產品(只有一個) 我開始查詢:WordPress的簡單查詢

<?wpsc_start_category_query(array('category_group'=> get_option('wpsc_default_category'))); ?> 

,然後嘗試使用get_posts()函數來獲取產品:

$args = array(
'post_type' => 'wpsc-product', 
'posts_per_page' => 1, 
'tax_query' => array(
    array(
    'taxonomy' => 'wpsc_product_category', 
    'field' => 'id', 
    'terms' => $aka 
))); 
$cat1_posts = get_posts($args); 

其中$又名是:

$aka = '[wpsc_category_id]'; 

但是當我回顯$ cat1_posts [0] - > ID;它只顯示每個類別的最後一個產品ID。問題是什麼?只回聲[wpsc_category_id]的作品完美。 最近幾天我嘗試了一切。我會買你的cookies的幫助

我得想法,我需要的foreach或任何類似這樣的

+0

不可能沒有人能幫 – CBeTJlu4ok 2013-02-20 23:07:16

回答

1

可以使用get_terms()函數。因此,像這樣(未經)

<?php 
    //for each category, show latest post 
    $cat_args=array(
     'orderby' => 'name', 
     'order' => 'ASC' 
     ); 
    $categories = get_terms('wpsc_product_category'); 
     foreach($categories as $category) { 
     $args=array(
      'showposts' => 1, 
      'post_type' => 'wpsc-product', 
      'wpsc_product_category' => array($category->slug) 
     ); 
     $posts=get_posts($args); 
      if ($posts) { 
      echo '<p>Category: <a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name.'</a> </p> '; 
      foreach($posts as $post) { 
       setup_postdata($post); ?> 
       <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
       <?php 
      } // foreach($posts 
      } // if ($posts 
     } // foreach($categories 
    ?> 
+0

非常感謝你!!!!!!!!!!!!非常非常等)) 工程完美,如果我們不計算類別圖像,但通常的類別沒有縮略圖,而我的wpsc類別。所以這是不同的問題,我猜 k kyou – CBeTJlu4ok 2013-02-21 11:02:03

+0

Ammmm no does not no work,I tought it was working,but actually it does same same my code。只查詢每個類別的最後一個產品。 即使它不是這一類 – CBeTJlu4ok 2013-02-21 13:05:07

+0

我改變 「wpsc_product_category」 =>陣列($分類 - >蛞蝓)到 「wpsc_product_category」 => $類別 - >塞 中,現在只 – CBeTJlu4ok 2013-02-21 13:46:49