【发布时间】:2015-01-21 03:12:15
【问题描述】:
我有一个 PHP 函数,它以 PHP list() 格式返回值。此函数返回一个 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 的 checked() 函数,我试图返回分类法的初始列表,其中保存在数据库中的值标记为 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 in C:\xampp\htdocs\wpbp-admin.php 上线...
对于复选框列表中未选中的每个项目,它都会给我这个警告。
我希望代码不返回这些警告。我知道这是使用 isset 的问题,但我不确定将它放在哪里。
非常感谢任何帮助,谢谢。
【问题讨论】:
标签: php arrays wordpress foreach