2011-09-30 95 views
0

我在Wordpress中使用MagicFields,其中包含重複的成分的自定義組。成分類型字段用單選按鈕選擇。爲Wordpress模板條件查找數組中的值

我想寫一個條件語句只顯示某些成分類型(基礎,醬等),以便它們可以顯示在頁面上的不同列表中。

的我想要實現的一個例子是:

if (in_array('Base', $IngGroup)) { 
    echo "Base Ingredients"; 
} 
elseif (in_array('Sauce', $IngGroup)) { 
    echo "Sauce Ingredients"; 
} 

這裏是PR($ IngGroup)輸出的陣列;

Array 
(
[1] => Array 
    (
     [ingredient_type] => Array 
      (
       [1] => Main 
      ) 
     [ingredient_unit] => Array 
      (
       [1] => g 
      ) 
     [ingredient_amount] => Array 
      (
       [1] => 300 
      ) 
     [ingredient_name] => Array 
      (
       [1] => Chicken 
      ) 
    ) 
[2] => Array 
    (
     [ingredient_type] => Array 
      (
       [1] => Sauce 
      ) 
     [ingredient_unit] => Array 
      (
       [1] => g 
      ) 
     [ingredient_amount] => Array 
      (
       [1] => 220 
      ) 
     [ingredient_name] => Array 
      (
       [1] => Sauce 
      ) 
    ) 
) 

回答

1
foreach($IngGroup as $Ing) { 
    if($Ing[ingredient_type][1] == 'Sauce') { 
     echo "Sauce Ingredients"; 
    } elseif ($Ing[ingredient_type][1] == 'Base') { 
     echo "Base Ingredients"; 
    } 
}