我的條款有一個額外的 ACF 真/假欄位。我需要獲取啟用該欄位的所有術語 ID 的串列,并回傳這些 ID 的串列。
到目前為止,我已經實作了這樣的代碼,但它只回傳一個 ID:
function terms_exclude_id_func()
{
$terms_control = get_terms([
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_query' => [
[
'key' => 'glob_menu',
'value' => '1',
]
]
]);
foreach ($terms_control as $term_control) {
$IDs = $term_control->term_id;
}
return $IDs;
}
echo terms_exclude_id_func();
我需要echo terms_exclude_id_func();回傳這樣的東西688, 534, 827
我將不勝感激在這件事上的任何幫助。我只是在學習自己)
uj5u.com熱心網友回復:
在您的 foreach 回圈中,$IDs 必須是一個陣列,并且您需要將值推入其中,而不是重新定義其值:
function terms_exclude_id_func()
{
$terms_control = get_terms([
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_query' => [
[
'key' => 'glob_menu',
'value' => '1',
]
]
]);
$IDs = array(); // define the variable as an array
foreach ($terms_control as $term_control) {
$IDs[] = $term_control->term_id; // push to the array
}
return $IDs;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/438312.html
上一篇:我如何找到兩個具有特定名稱的員工一起作業的專案數量?(使用LIKE運算子)
下一篇:函式結果中的排名不匹配
