Woocommerce类别限制

时间:2018-06-26 14:26:28

标签: php wordpress woocommerce

我想知道如何限制wordpress woocommerce类别的显示。我只想显示前3个类别,但是此代码显示了所有类别。我不想显示水球类别

 `<?php
    $taxonomy     = 'product_cat';
    $orderby      = '';  
    $show_count   = 0;     
    $pad_counts   = 0;     
    $hierarchical = 1;     
    $title        = '';  
    $empty        = 0;
    $args = array(
        'taxonomy'     => $taxonomy,
        'orderby'      => $orderby,
        'show_count'   => $show_count,
        'pad_counts'   => $pad_counts,
        'hierarchical' => $hierarchical,
        'title_li'     => $title,
        'hide_empty'   => $empty,
    );
    $all_categories = get_categories( $args );
    foreach ($all_categories as $cat) {
        if($cat->category_parent == 0) {
            $category_id = $cat->term_id;
            $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
            $image = wp_get_attachment_url( $thumbnail_id );
            ?>`

我只想在首页上显示3个类别(随机选择其中任何一个)

1 个答案:

答案 0 :(得分:4)

您需要添加数字参数以限制类别。

'number'       => 3,

您的完整代码

<?php
    $taxonomy     = 'product_cat';
    $orderby      = '';  
    $show_count   = 0;     
    $pad_counts   = 0;     
    $hierarchical = 1;     
    $title        = '';  
    $empty        = 0;
    $args = array(
        'taxonomy'     => $taxonomy,
        'orderby'      => $orderby,
        'show_count'   => $show_count,
        'pad_counts'   => $pad_counts,
        'hierarchical' => $hierarchical,
        'title_li'     => $title,
        'hide_empty'   => $empty,
        'number'       => 3,
    );
    $all_categories = get_categories( $args );
    foreach ($all_categories as $cat) {
        if($cat->category_parent == 0) {
            $category_id = $cat->term_id;
            $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
            $image = wp_get_attachment_url( $thumbnail_id );
?>