【问题标题】:Showing only WooCommerce main Product Categories仅显示 WooCommerce 主要产品类别
【发布时间】:2017-07-21 14:06:28
【问题描述】:

这是我为视觉作曲家制作的简码。它存储了我的 woocommerce 类别,但我想不出我会如何停止它显示子类别而只显示主要类别。

有什么想法吗?

谢谢

<?php //wp_enqueue_script(array('jquery', 'owl-carousel'));
   $count = 0;
   $term_args = array('hide_empty' => $empty , 'number' => $num , 'order_by' => $sort , 'order' => $order);
   //if( $cat ) $query_args['category_name'] = $cat;
   //echo balanceTags($cat); exit('sssss');
   $terms = get_terms( 'product_cat', $term_args) ; 
   //printr($terms);
   ob_start() ;?>

<section id="featureCat2" class="row contentRowPad">
<div class="container">
    <h3 class="heading"><?php echo balanceTags($title); ?></h3>
<?php if ( ! empty( $terms ) && ! is_wp_error( $terms ) ):?>
    <div class="row m0">

    <?php foreach ( $terms as $term ) :
            $meta = _WSH()->get_term_meta( '_sh_product_cat_settings', $term->term_id );//printr($meta);
    ?>

        <div class="col-sm-3">
            <div class="row category2 text-center">
                <div class="row m0 imgHov">
                <?php   $thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true ); ?>
                    <?php echo wp_get_attachment_image( $thumbnail_id, '292x198' ); ?>
                    <div class="hovArea row m0">
                        <a href="<?php echo esc_url(get_term_link($term));  ?>"><?php esc_html_e('shop now ', 'furniture');?><i class="fa fa-caret-right"></i></a>
                    </div>
                </div>
                <div class="row m0">
                    <h5 class="heading"><?php echo balanceTags($term->name);?></h5>
                    <ul class="list-unstyled black-color">
                    <?php $query = get_posts(array('showposts'=>4, 'post_type'=>'product', 'product_cat'=>$term->slug, 'order'=>'DESC')); //printr($query);?>
                        <?php if( $query )
                        foreach( $query as $qu ): ?>
                            <li><a href="<?php echo get_permalink($qu->ID); ?>" title="<?php echo get_the_title($qu->ID); ?>"><?php echo get_the_title($qu->ID); ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>
        </div>

        <?php endforeach;?>

    </div>

    <?php endif;?>

</div>
</section>

<?php return ob_get_clean();

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    要仅显示主要产品类别,您需要以这种方式添加 'parent' 参数:

    $term_args = array(
        'parent' => 0,
        'hide_empty' => $empty , 
        'number' => $num , 
        'order_by' => $sort , 
        'order' => $order,
    );
    
    $terms = get_terms( 'product_cat', $term_args);
    

    那么您将只获得主要产品类别而没有子类别。

    【讨论】:

    • 请注意,如果您的新答案已从您的所有答案中删除,您该怎么办。有关更多信息,请参阅:meta.stackoverflow.com/questions/352638/…
    • 工作得很好,谢谢!只是想知道,这些术语是否在 woocommerce 文档中的某个位置以供将来参考?干杯
    • @JPWeb 所有这些都与 WordPress 自定义分类法和 WP_Query class reference... 有关
    猜你喜欢
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多