programing

WooCommerce에서 모든 제품 카테고리 가져오기

javamemo 2023. 3. 6. 20:36
반응형

WooCommerce에서 모든 제품 카테고리 가져오기

WooCommerce에서 상품 카테고리를 취득하려고 하는데get_terms()기능을 사용할 수 없습니다.빈 어레이가 표시됩니다.

내가 뭘 잘못하고 있지?모든 Woocommerce 제품 카테고리 용어를 입수하려면 어떻게 해야 합니까?

제품 카테고리는 WooCommerce 제품이 사용하는 "커스텀 분류법"입니다.

를 사용해야 합니다.get_terms()올바른 분류법을 사용하면 다음과 같습니다.

// Get Woocommerce product categories WP_Term objects
$categories = get_terms( ['taxonomy' => 'product_cat'] );

// Getting a visual raw output
echo '<pre>'; print_r( $categories ); echo '</pre>';

빈 제품 카테고리는 다음을 사용하여 얻을 수도 있습니다.get_terms()예를 들어 다음과 같습니다.

$categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );

테스트 및 동작 (WordPress 3.5+ 및 WooCommerce 2.4+)… 둘 다 고객에게 적합합니다.

다음과 같은 결과를 얻을 수 있습니다.

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 83
            [name] => Uncategorized
            [slug] => uncategorized
            [term_group] => 0
            [term_taxonomy_id] => 83
            [taxonomy] => product_cat
            [description] => 
            [parent] => 0
            [count] => 5
            [filter] => raw
            [meta_value] => 1
        )

    [2] => WP_Term Object
        (
            [term_id] => 11
            [name] => Tshirts
            [slug] => tshirts
            [term_group] => 0
            [term_taxonomy_id] => 11
            [taxonomy] => product_cat
            [description] => 
            [parent] => 0
            [count] => 13
            [filter] => raw
            [meta_value] => 2
        )
    // … and so on …
)

모든 제품 범주에서 내 범주를 제거했으며 표시하지 않을 범주도 제외했습니다.

$taxonomy = "product_cat";
$terms = get_terms($taxonomy, array('orderby' => 'slug', 'hide_empty' => false, 'exclude' => array( 19 ))); //Exclude Specific Category by ID

foreach ($terms as $term) {
    $thumbnail_id = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
    $image = wp_get_attachment_url($thumbnail_id); ?>

    <div class="col-12 col-md-3">
        <div class="cat-item drop-shadow white-b padding-20 align-center margin-bottom-30">
            <?php 
                echo '<h3 class="uppercase strong blue-2-c margin-bototm-20 equal-height">' . $term->name . '</h3>';
                echo '<div class="item-thumbnail" style="background: url('.$image.');height:150px"></div>'; 
                echo '<a href="' . get_term_link($term->name, $taxonomy) . '" class="button color-blue-2-radial">View Range</a>';
            ?>
        </div>
    </div>

언급URL : https://stackoverflow.com/questions/55650785/get-all-product-categories-in-woocommerce

반응형