简体   繁体   中英

Displaying categories in order by name - wordpress

Use wordpress and I want displaying categories in order by name.

I pasted the function in file archive-product.php but displays the categories in order after ID

< ul class = "products" >
    <? php woocommerce_product_subcategories;? >
</ul>

So, modified the file wc-template-functions.php, but can not displaying by order name.

See the code:

NOTE: using child_of instead of parent-this is not ideal due to the silverside WP bug (https://core.trac.wordpress.org/ticket/15626) pad_counts won't work
$product _ categories = get_categories (apply_filters (' woocommerce_product_subcategories_args ', array (
' parent ' = $parent > _ id,
' menu_order ' = > ' ASC ',
' hide_empty ' = > 0
' hierarchical ' = > 1
' taxonomy ' = product_cat > ' ',
' pad_counts ' = > 1
) ) );

Try this code:

$args = array(
    'order' => 'ASC', //or DESC
    'orderby' => 'name',
    'parent' => 0
); 
$terms = get_terms( 'product_cat', $args );

if ( $terms ) {
    echo '<ul class="products">';
    foreach ( $terms as $term ) {
         echo '<li>' . $term->name . '</li>';
    }
    echo '</ul>';
}

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