2016-04-01 271 views
0

我已经创建了一些自定义类别的产品,它有子类别和子类别有更多的子类别。现在首先显示主要类别。如果我在那里显示子,那么显示与该类别及其子类别相关的所有子类别。我想逐步显示它们。也就是说,如果用户点击主类别然后进入其子类别页面。如果用户点击其中一个子类别,则应该转到子类别,如果没有子类别,则显示该产品。代码是类别和子类别wordpress

$products = get_term_children($term_id[0], 'product-cat'); 
if(count($products) > 0){ 
    $count = 0; 
    $sorted_products = array(); 

    foreach ($products as $product) { 

     $sorted_products = get_term($product, 'product-cat'); 
     $prod_meta = get_option("taxonomy_term_".$term->term_id); 
    //echo "<pre>"; print_r($sorted_products); 

    foreach ($sorted_products as $product) { ?> 
      <div class="col-md-3 col-sm-4 col-xs-12"> 
        <a href="<?php echo $product['link']; ?>"> 
         <a href="<?php echo $product['link']; ?>" class="hvr-grow"> 
          <img class="center-block img-responsive" src="<?php echo $product['img'] ? $product['img'] : '/wp-content/themes/ruskin/images/dummy-product.jpg'; ?>" alt="<?php echo $product['name']; ?>"> 
        <h3><a href="<?php echo $product['link']; ?>"><?php echo $product['name']; ?></a></h3> 
else{ 

    # Define the WP Query post arguments. 
    $args = array(
    'post_status' => 'publish', 
    'post_type' => 'products', 
    'posts_per_page' => -1, 
    //'meta_query' => array('relation' => 'AND', array('key' => '_cus__featured', 'value' => '1', 'compare' => '='),), 
    'meta_key' => '_cus__sort_order', 
    //'meta_value' => 'meta_value', 
    'orderby' => 'meta_value_num', 
    'order' => 'ASC', 
    'tax_query' => array(
     array('taxonomy' => 'product-cat', 
      'field' => 'slug', 
      'terms' => $cats 
     ))); 
$loop = new WP_Query($args); 
$total = $loop->found_posts; 
$sliders=''; 
// Generatet the slider conteents 
while ($loop->have_posts()) { 
    $loop->the_post(); 
    $listingimg = get_post_custom_values('_cus__listing_img'); 
     $listingimg = "/wp-content/themes/bodyo/images/no-slider-img.jpg"; 

    $img = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'main_slide_img'); 
     $img = "/wp-content/themes/bodyo/images/no-slider-img.jpg"; 
    $sliders .= '<a href="'. get_the_permalink() .'" class="hvr-grow">'; 
    $sliders .= '<img src="'.$listingimg.'" class="center-block img-responsive" alt="'. get_the_title() .'" />'; 
    $sliders .= '</a>'; 
    $sliders .= '</div>'; 
    $sliders .= '<a href="'. get_the_permalink() .'">'; 
    $sliders .= '<h3>'. get_the_title() .'</h3>'; 
    $sliders .= '<p>'. get_the_excerpt() .'</p>'; 
    $sliders .= '<a href="'. get_the_permalink() .'">read more</a>'; 
    $counter++; 

} 

它覆盖了以前的排序顺序。也就是说,如果从仪表板中按排序顺序给出2到3个类别,那么它只显示最后一个。前两个被覆盖。

回答

0

像这样的东西应该这样做。

$args = array(
    'child_of' => $term_id[0], 
    'taxonomy' => 'product-cat', 
    'hierarchical' => true, 
    'depth' => 1, 
); 
$categories = get_categories($args); 

记住get_categories()get_terms()的包装。

您可以找到所有可接受的值here但您正在寻找的值是深度。

+0

在哪个步骤中可以详细说明这一点 –

+0

您发布的代码的第一行。 –