WooCommerce:列出所有类别


9

我将WooCommerce插件与WordPress结合使用,并且在我的主题中,我想使用PHP在导航菜单中列出所有类别。

我试过使用 woocommerce_product_categories();

但是我不想要图像或其他HTML元素,而只是它们的名称(也许还有永久链接)。

我如何获得这些数据?

Answers:


23

从相同的功能中获取:

// 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);

将为您提供产品类别列表。简单!


7
太棒了!谢谢。只是为了让所有读者都知道,只需在下面添加一个foreach: foreach( $product_categories as $cat ) { echo $cat->name; }
Edd Turtle
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.