2013-11-27 49 views
1

我有下面的代碼:如何在數組中添加2個也是數組的變量?

這裏我聲明的代碼用於獲取類別名稱和蛞蝓

$args = array(
      'orderby' => 'name', 
      'order' => 'ASC', 
      'hide_empty' => 0, 
     'taxonomy'   => 'category' 
     ); 
     $categories = get_categories($args); 
     $categories_name = array(); // here i get the category name 
    $categories_ids = array(); // here i get the category slug 
    foreach($categories as $category){ 
      $categories_name[] = $category->name; 
     $categories_ids[] = $category->slug; 
     } 

這裏是我的後端迴盪着的類別名稱爲用戶設置選擇

$this->settings['categoriesmain3'] = array(
      'section' => 'general', 
      'title' => __('Select Right Block Category'), 
      'desc' => __('Here you can select the main category for main right block.'), 
      'type' => 'select', 
      'std'  => '', 
      'choices' => array('$categories_name' => '$categories_ids') //I am trying to do 
     ); 
    $settings = get_option('mytheme_options'); 
    $my_choices_cat3 = $settings['categoriesmain3']; 

我試圖在choices =>代碼array('$categories_name' => '$categories_ids')'添加,但它不喜歡的工作。在我手動添加選擇字段的例子中,我有'choices' => array('Choice 1' => 'choice1', 'Choice 2' => 'Choice 2'),那麼我想添加一個動態列表的choices元素的正確語法是什麼?

回答

2

這可以通過創建一個數組,使用類別名稱作爲鍵和類別ID作爲值來解決。

$categories = get_categories($args); 
$categories_options = array(); 

foreach($categories as $category){ 
    $categories_options[$category->name] = $category->slug; 
} 

然後,您可以使用以下行添加列表。

'choices' => $categories_options 
+0

這正是我一直在尋找的感謝。 – Adrian

相關問題