2015-01-21 143 views
0

我有一个PHP函数返回PHP列表()格式的值。该函数返回一个WordPress分类法列表,其中包含手动添加的一些自定义条目。如何使用和访问保存的数组值

/** 
* Returns array with taxonomies 
*/ 
public function get_taxonomies(){ 
    $args = array(
     'public' => true, 
     '_builtin' => false 
    ); 
    $taxonomies = get_taxonomies($args, 'objects'); 
    $list = array(); 
    $list[__('Category', $this->prefix)] = 'category'; 
    $list[__('Tag', $this->prefix)] = 'post_tag'; 
    foreach($taxonomies as $tax){ 

     $list[$tax->labels->name] = $tax->name; 
    } 
    return $list; 
} 

该函数返回以下数组值:

Array 
(
    [Category] => category 
    [Tag] => post_tag 
    [Product Categories] => product_cat 
    [Product Tags] => product_tag 
    [Shipping Classes] => product_shipping_class 
) 

阵列的第一部分是该分类名和第二分类法值。

我将这些值显示为复选框列表,并将它们作为数组保存在我的数据库中。

这是我保存在数据库中的复选框列表的数据。这是返回什么,如果我的print_r()保存数据列表:

Array 
(
    [category] => category 
    [post_tag] => post_tag 
    [product_cat] => product_cat 
) 

使用WordPress的检查()函数我试图返回分类与保存在标记为数据库中的值的初始列表HTML“已选中”。

这是我曾尝试:

// This is where I am trying to get at the saved values 
$multi_stored = $options[$id]; 

$taxonomies = $this->get_taxonomies(); 
foreach($taxonomies as $name => $label){ 

    $check_key = $id . '_' . $option; 

    $output .= '<input class="checkbox'.$field_class.'" type="checkbox" name="'.$this->prefix.'_options['.$id.']['.$label.']" id="'.$check_key.'" value="'.$label.'" '.checked($multi_stored[$label], $label, false).' /><label for="'.esc_attr($name).'">'.esc_html($name).'</label><br />'; 
} 

当我运行此代码,我收到了以下PHP警告。

注意:未定义指数:product_tag在C:\ XAMPP \ htdocs中\ wpbp-admin.php的就行...

它给我这个警告的每个项目的复选框不被选中的列表。

我想代码不会返回这些警告。我知道这是使用isset的问题,但我不确定将它放在哪里。

任何帮助非常感谢,谢谢。

回答

0

我得到它的工作。

我更改环路“检查”功能,此:

$multi_stored = $options[$id]; 

$taxonomies = $this->get_taxonomies(); 
foreach($taxonomies as $name => $label){ 

    if(isset($multi_stored[$label])){ 
     if($label == $multi_stored[$label]){ 
      $checked = 'checked="checked"'; 
     } else { 
      $checked = ''; 
     } 
    } else { 
     $checked = ''; 
    } 

    $check_key = $id . '_' . $label; 

    $output .= '<input class="checkbox'.$field_class.'" type="checkbox" name="'.$this->prefix.'_options['.$id.']['.$label.']" id="'.$check_key.'" value="'.$label.'" '.$checked.' /><label for="'.esc_attr($name).'">'.esc_html($name).'</label><br />'; 
}