简体   繁体   中英

categories and sub categories wordpress

I have created some custom categories for products and it has sub categories and the sub categories has further sub categories. now first i display the main categories. If i display there sub then all the sub categories related to that category and its sub category are displayed.I want to show them step by step.That is if the user click on main category then it goes to its sub category page. If user click one of its sub category then it should go to sub categories and if has no sub category then displayed the products. Code is this

 $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++;

}

It over-rites the previous sort order. That is if from dashboard we give 2 to three categories in sort order then it displays just the last one. First two are overwritten.

Something like this should do it.

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

Keep in mind that get_categories() is a wrapper of get_terms() .

You can find all the accepted values here but the one you're looking for is depth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM