簡體   English   中英

僅使用get_terms獲得類別名稱

[英]get the category names only, with get_terms

<?php  
  $categories = array('bathroom', 'bar');  

    foreach ($categories as $category) {
        $args = array( 'post_type' => 'product', 'posts_per_page' => 3, 'product_cat' => $category, 'orderby' => 'rand' );
?>

我想使上面的功能更具動態性。 這樣,我就不必手動將特定類別添加到$ categories變量中。 相反,我需要只獲取(woocommerce產品)類別名稱的東西,並將它們放置在數組中。

我確實得到了這個清單:

$category_list_items = get_terms( 'product_cat' );

foreach($category_list_items as $category_list_item){
    echo $category_list_item->name;
}

但是我不知道如何將此列表添加到第一個函數中。 有誰知道如何做到這一點?

謝謝!

根據我對問題的理解,您正在將$category_list_items填充$category_list_items應用程序中category_list_item對象的數組。 結果,您可以將回聲轉換為類別名稱數組,然后將其傳遞給函數

$categories = []; //array to store all of your category names
$category_list_items = get_terms( 'product_cat' );

foreach($category_list_items as $category_list_item){
    if(! empty($category_list_item->name) ){
        array_push($categories, $category_list_item->name);
    }
}

一旦運行了此foreach,現在將填充一個categorys數組,以傳遞給上述函數。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM