2013-02-20 137 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