在滑块内显示Woocommerce产品类别

时间:2019-02-18 12:02:54

标签: wordpress woocommerce shortcode owl-carousel

我试图在滑块内显示所有产品类别

<div id="category-wrapper">                     
    <div id="owl1" class="owl-carousel">
        <div class="item">
            <?php echo do_shortcode('[product_category category="main-cat"]'); ?>
        </div>  

        <div class="item">
            <?php echo do_shortcode('[product_category category="cat2"]'); ?>
        </div>

        <div class="item">
            <?php echo do_shortcode('[product_category category="cat3"]'); ?>
        </div>

        <div class="item">
            <?php echo do_shortcode('[product_category category="cat4"]'); ?>
        </div>

        ...
    </div>
</div>

但是它输出这些类别而不是类别中的产品。

我需要这样做: link image每个框代表根中的一个产品类别。

1 个答案:

答案 0 :(得分:0)

我自己做的,但是在子类别中,图像不起作用:

<div id="kategorie-nahlad">

    <?php
        $taxonomy     = 'product_cat';
        $orderby      = 'name';  
        $show_count   = 0;
        $pad_counts   = 0;
        $hierarchical = 1;
        $title        = '';  
        $empty        = 0;

        $args = array(
            'taxonomy'     => $taxonomy,
            'orderby'      => $orderby,
            'show_count'   => $show_count,
            'pad_counts'   => $pad_counts,
            'hierarchical' => $hierarchical,
            'title_li'     => $title,
            'hide_empty'   => $empty
        );

        $all_categories = get_categories( $args ); ?>

    <div id="owl1" class="owl-carousel">

        <? 
        foreach ($all_categories as $cat) {
            $thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
            $image = wp_get_attachment_url( $thumbnail_id );
            $image_alt = get_post_meta( $image->id, '_wp_attachment_image_alt', true );

            if ($cat->category_parent == 0) {
                $category_id = $cat->term_id;
                echo '<div class="item text-center">';
                echo '<br><a href="'. get_term_link($cat->slug, 'product_cat') .'"><img src="' . $image . '" alt="' . $image_alt . '">'. $cat->name .'</a>';
                echo '</div>';

                $args2 = array(
                    'taxonomy'     => $taxonomy,
                    'child_of'     => 0,
                    'parent'       => $category_id,
                    'orderby'      => $orderby,
                    'show_count'   => $show_count,
                    'pad_counts'   => $pad_counts,
                    'hierarchical' => $hierarchical,
                    'title_li'     => $title,
                    'hide_empty'   => $empty
                );

                $sub_cats = get_categories( $args2 );
                if($sub_cats) {
                    foreach($sub_cats as $sub_category) {
                        $thumbnail_id_sub = get_term_meta( $cat->term_id, 'thumbnail_id', true );
                        $image_sub = wp_get_attachment_url( $thumbnail_id_sub );

                        echo '<div class="item text-center">';
                        echo '<br><a href="'. get_term_link($sub_category->slug, 'product_cat') .'"><img src="' . $image . '" alt="' . $image_alt . '">'. $sub_category->name .'</a>';
//                                          echo apply_filters( 'woocommerce_subcategory_count_html', ' <div class="cat-count">' . $sub_category->count . '</div>', $category );
                        echo '</div>';
                    }
                }
            }
        }
        ?>

    </div><!-- KONIEC OWL1 -->
</div>