类别和子类别wordpress

时间:2016-04-01 08:51:19

标签: php wordpress sorting taxonomy custom-taxonomy

我为产品创建了一些自定义类别,它有子类别,子类别还有其他子类别。现在我首先显示主要类别。如果我在那里显示sub,则显示与该类别及其子类别相关的所有子类别。我想逐步显示它们。也就是说,如果用户点击主类别,那么它将转到其子类别页面。如果用户单击其子类别之一,则应转到子类别,如果没有子类别,则显示产品。代码是这个

 $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个类别,那么它只显示最后一个。前两个被覆盖。

1 个答案:

答案 0 :(得分:0)

这样的事情应该这样做。

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

请注意,get_categories()get_terms()的包装。

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