開發者:wordpress
我在網上得到了這段代碼,它非常適合從串列中排除一個類別 ID...但是當我嘗試排除多個類別時,它只會選擇第一個
function list_role_term_exclusions($exclusions,$args) {
$tax_type1 = 'category'; //string (word variable)
$cat_array = array(106, 108); //array (set) of integers (number variables)
foreach( $cat_array as $cat_id_value )
{
$children_of_parent_cat = implode(',',get_term_children($cat_id_value,$tax_type1)); // get all child categories
$children_of_parent_cat = (empty($children_of_parent_cat) ? '' : ",$children_of_parent_cat"); // if childs empty or not
return $exclusions . " AND (t.term_id NOT IN ($cat_id_value ".$children_of_parent_cat." ))"; // Exclude parent and all child cats
}
unset($cat_id_value);
}
所以使用這個函式,它將成功地從類別中排除 ID 106...但不是 108(陣列中的第二個),但我的目標是我希望 $exclusions 包含 106 和 108 以便它們可以被排除
如果有辦法在頂部分配變數,例如類別 ID = 11,12,13 標簽 ID = 34,25,66
以便它可以排除特定的類別 ID 和分類 ID
uj5u.com熱心網友回復:
如果你有return內部foreach,它將中止回圈的其余部分,這意味著你只會從第一次迭代中獲得值。
將您的代碼更改為:
foreach( $cat_array as $cat_id_value )
{
$children_of_parent_cat = implode(',',get_term_children($cat_id_value,$tax_type1)); // get all child categories
$children_of_parent_cat = (empty($children_of_parent_cat) ? '' : ",$children_of_parent_cat"); // if childs empty or not
// Remove the return and only append the string
$exclusions .= " AND (t.term_id NOT IN ($cat_id_value ".$children_of_parent_cat." ))"; // Exclude parent and all child cats
}
return $exclusions;
現在它在回圈完成之前不會回傳字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/419697.html
標籤:
