1

嘿伙计们我有一个可怕的问题,基本上我想在一个名为“sermon_series”的自定义分类法中创建一个元字段,我可以在其中链接图像。一切都已创建,但它不会保存。我尝试了许多其他代码变体,但没有一个有效。感谢您的帮助,这是我的代码:

<?php
//add extra fields to custom taxonomy edit form callback function
function extra_tax_fields($tag) {
   //check for existing taxonomy meta for term ID
    $t_id = $tag->term_id;
    $term_meta = get_option( "taxonomy_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Category Image Url'); ?></label></th>
<td>
<input type="text" name="term_meta[img]" id="term_meta[img]" size="3" style="width:60%;" value="<?php echo $term_meta['img'] ? $term_meta['img'] : ''; ?>"><br />
            <span class="description"><?php _e('Image for Term: use full url with http://'); ?></span>
        </td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra1"><?php _e('extra field'); ?></label></th>
<td>
<input type="text" name="term_meta[extra1]" id="term_meta[extra1]" size="25" style="width:60%;" value="<?php echo $term_meta['extra1'] ? $term_meta['extra1'] : ''; ?>"><br />
            <span class="description"><?php _e('extra field'); ?></span>
        </td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra2"><?php _e('extra field'); ?></label></th>
<td>
<input type="text" name="term_meta[extra2]" id="term_meta[extra2]" size="25" style="width:60%;" value="<?php echo $term_meta['extra2'] ? $term_meta['extra2'] : ''; ?>"><br />
            <span class="description"><?php _e('extra field'); ?></span>
        </td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra3"><?php _e('extra field'); ?></label></th>
<td>
            <textarea name="term_meta[extra3]" id="term_meta[extra3]" style="width:60%;"><?php echo $term_meta['extra3'] ? $term_meta['extra3'] : ''; ?></textarea><br />
            <span class="description"><?php _e('extra field'); ?></span>
        </td>
</tr>
<?php
}

// save extra taxonomy fields callback function
function save_extra_taxonomy_fields( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id");
        $cat_keys = array_keys($_POST['term_meta']);
            foreach ($cat_keys as $key){
            if (isset($_POST['term_meta'][$key])){
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        //save the option array
        update_option( "taxonomy_$t_id", $term_meta );
    }
}

add_action( 'sermon_series_edit_form_fields', 'extra_tax_fields', 10, 2);
add_action( 'edited_sermon_series', 'save_extra_fields_callback', 10, 2);
?>
4

1 回答 1

0

您还需要一个钩子来创建新的分类法。

add_action( 'edited_sermon_series', 'save_extra_fields_callback', 10, 2);

将处理您的 sermon_series -> "Edit Sermon Series" 的编辑。

add_action( 'created_sermon_series', 'save_extra_fields_callback', 10, 2);

将在您第一次创建新的讲道系列时保存 -> “添加讲道系列”。确保将两者都包含在您的代码中。

于 2012-07-11T18:19:29.393 回答