13

I'm using the WooCommerce plugin with WordPress and within my theme I'd like to list all categories within a navigation menu with PHP.

I've tried using woocommerce_product_categories();

but I don't want the images, or other HTML elements, just their names (and maybe permalinks).

How can I get that data?

1 Answer 1

31

taken from that very same function:

// prior to wordpress 4.5.0
$args = array(
    'number'     => $number,
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
    'include'    => $ids
);

$product_categories = get_terms( 'product_cat', $args );

// since wordpress 4.5.0
$args = array(
    'taxonomy'   => "product_cat",
    'number'     => $number,
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
    'include'    => $ids
);
$product_categories = get_terms($args);

will give you the list of product categories. easy!

2
  • 8
    Awesome! Thank you. Just to clear things up for any readers, just add a foreach after: foreach( $product_categories as $cat ) { echo $cat->name; }
    – Edd Turtle
    Commented Apr 2, 2013 at 10:42
  • how to print categories with subcategories. LIke having parent and under parent list of it's categories Commented Jul 12, 2021 at 10:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.