Woocommerce类别按菜单使用get_categories排序

时间:2013-09-10 07:23:50

标签: php wordpress get woocommerce categories

我正在调用一个Woocommerce类别的列表,并且我试图让他们在自定义,darg-and-drop中显示他们已经组织但没有用的订单。通常的'orderby'=> 'menu_order'无效。代码如下:

         <?php
            $args=array(
                'orderby' => 'menu_order',
                'order' => 'ASC',
                'child_of' => 13,
                'hide_empty' => 0,
                'taxonomy' => 'product_cat'
            );
            $categories=get_categories($args);
                foreach($categories as $category) {  
                    echo "<li class='filter-option " . $category->slug . "'><a href='#' data-filter-value='." . $category->slug . "'>";
                    echo $category->name;
                    echo "</a></li>";
                } 
            ?>

非常感谢任何帮助

2 个答案:

答案 0 :(得分:4)

您要查找get_categories()参数的参数是

'menu_order' => 'asc'

所以,例如。我的功能是返回前三个根Woocommerce类别,排除未分类的外观如下:

// load first three categories from Woocommerce
function my_get_woocommerce_categories() {
	$args = array(
		'taxonomy' => 'product_cat',
		'parent' => 0,
		'hide_empty' => false,
		'number' => 3,
		'show_uncategorized' => false,
		'menu_order' => 'asc',
	);

	$categories = get_categories( $args );

	return $categories;
}

答案 1 :(得分:1)

请试试这个......

<?php $wcatTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC',  'parent' =>0)); //, 'exclude' => '17,77'
                foreach($wcatTerms as $wcatTerm) : 
                    $wthumbnail_id = get_woocommerce_term_meta( $wcatTerm->term_id, 'thumbnail_id', true );
                    $wimage = wp_get_attachment_url( $wthumbnail_id );
                ?>
                <ul>
                    <li class="libreak"><?php if($wimage!=""):?><img src="<?php echo $wimage?>"><?php endif;?></li>
                    <li>
                        <a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a>
                        <ul class="wsubcategs">
                        <?php
                        $wsubargs = array(
                           'hierarchical' => 1,
                           'show_option_none' => '',
                           'hide_empty' => 0,
                           'parent' => $wcatTerm->term_id,
                           'taxonomy' => 'product_cat'
                        );
                        $wsubcats = get_categories($wsubargs);
                        foreach ($wsubcats as $wsc):
                        ?>
                            <li><a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a></li>
                        <?php
                        endforeach;
                        ?>  
                        </ul>
                    </li>
                </ul>
            <?php 
                endforeach; 
            ?>