【问题标题】:Woocommerce Categories LimitWoocommerce 类别限制
【发布时间】:2018-06-26 14:26:27
【问题描述】:

我想知道如何限制 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 个类别(其中任何一个都是随机的)

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2019-06-22
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      相关资源
      最近更新 更多