首页 > 解决方案 > 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 个类别(其中任何一个都是随机的)

标签: phpwordpresswoocommerce

解决方案


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

'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 );
?>

推荐阅读