我将一些自定义字段添加到带有true或false的选择框的类别中。但如果术语meta不存在,则所选函数会给出一个错误:
注意:未定义索引:everyoneCompose in/Applications/MAMP/。。。在线22
我理解为什么,但如何设置默认值以防止此错误?我试图检查metadata\\u是否存在,并添加metadata一词,但这不起作用,请在此处检查我的代码:
function addSelectdFieldsToCat(){
$cat_id = $_GET[\'tag_ID\'];
if (metadata_exists(\'term\', $cat_id, \'cat_settings\' )) {
$catSettings = get_term_meta($cat_id, \'cat_settings\', false);
} else {
$defaultdata = array( \'cat_settings\' =>
array(
\'everyoneCompose\' => true,
\'restrictedGroup\' => true
)
);
add_term_meta($cat_id, \'cat_settings\', $defaultdata, false);
}
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_page_title"><?php _e(\'Everyone can compose?\'); ?></label></th>
<td>
<select name="cat_settings[everyoneCompose]">
<option value="1" <?php selected( $catSettings[\'everyoneCompose\'], 1 ); ?>>true</option>
<option value="0" <?php selected( $catSettings[\'everyoneCompose\'], 0 ); ?>>false, alleen admin</option>
</select>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_page_title"><?php _e(\'Restricted category?\'); ?></label></th>
<td>
<select name="cat_settings[restrictedGroup]">
<option value="1" <?php selected( $catSettings[\'restrictedGroup\'], 1 ); ?>>true</option>
<option value="0" <?php selected( $catSettings[\'restrictedGroup\'], 0 ); ?>>false</option>
</select>
</td>
</tr>
<?php
}
add_action ( \'edit_category_form_fields\', \'addTitleFieldToCat\');
function saveCategoryFields() {
if ( isset( $_POST[\'cat_settings\'] ) ) {
update_term_meta($_POST[\'tag_ID\'], \'cat_settings\', $_POST[\'cat_settings\']);
}
}
add_action ( \'edited_category\', \'saveCategoryFields\');