有向类别编辑器添加自定义域的示例吗?

时间:2011-01-07 作者:Scott B

我想我很快就要搞定这个问题了:)

我正在尝试向类别编辑器添加一组自定义字段。由于我没有处理post-meta,我相信我会将自定义类别字段值写入wp\\u term\\u分类表,而不是wp\\u options表。这是正确的吗?

如果有任何关于如何做到这一点的示例,请共享一个链接或一段代码。我不知道如何捕获和保存自定义类别字段。

这是我的密码。。。

//add the hook to place the form on the category editor screen
add_action(\'edit_category_form\',\'ce4_category_admin\');

//Adds the custom title box to the category editor
function ce4_category_admin($category){
    echo "category: ".$category->term_id; //great, I got a reference to the term_id, now I need to get/set my custom fields with this key
    ?>
<table class="form-table">
        <tr class="form-field">
            <th scope="row" valign="top"><label for="_ce4-categoryTitle">Full Category Title</label></th>
            <td><input name="_ce4-categoryTitle" id="_ce4-categoryTitle" type="text" size="40" aria-required="false" value="<?php echo get_taxonomy($category->term_id, \'_ce4-categoryTitle\'); ?>" />
            <p class="description">The title is optional but will be used in place of the name on the home page category index.</p></td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="_ce4_fullDescription">Full Category Text for Landing Page</label></th>
            <td><textarea style="height:70px; width:100%;margin-left:-5px;" name="_ce4_fullDescription" id="_ce4_fullDescription"><?php echo get_taxonomy($category->term_id, \'_ce4_fullDescription\'); ?></textarea>
            <p class="description">This text will appear on the category landing page when viewing all articles in a category. The image, you supply above, if any, will be used here and this content will wrap around it.</p></td>
        </tr>
</table>
<?php
}

//How to save the custom field data? Normally I\'d use..
//add_action(\'save_post\', \'custom_save_function\');
//but this is not a post, so perhaps there\'s another method?

1 个回复
最合适的回答,由SO网友:onetrickpony 整理而成

不,你必须使用wp_options, 因为您无法在wp\\u term\\u taxonomy表中创建新字段(如果这样做,在下一次wp更新中,您将丢失这些字段)。

因此:

// the option name
define(\'MY_CATEGORY_FIELDS\', \'my_category_fields_option\');

// your fields (the form)
add_filter(\'edit_category_form\', \'my_category_fields\');
function my_category_fields($tag) {
    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);

    ?>

<table class="form-table">
        <tr class="form-field">
            <th scope="row" valign="top"><label for="_ce4-categoryTitle">Full Category Title</label></th>
            <td><input name="_ce4-categoryTitle" id="_ce4-categoryTitle" type="text" size="40" aria-required="false" value="<?php echo $tag_extra_fields[$tag->term_id][\'my_title\']; ?>" />
            <p class="description">The title is optional but will be used in place of the name on the home page category index.</p></td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="_ce4_fullDescription">Full Category Text for Landing Page</label></th>
            <td><textarea style="height:70px; width:100%;margin-left:-5px;" name="_ce4_fullDescription" id="_ce4_fullDescription"><?php  echo $tag_extra_fields[$tag->term_id][\'my_description\']; ?></textarea>
            <p class="description">This text will appear on the category landing page when viewing all articles in a category. The image, you supply above, if any, will be used here and this content will wrap around it.</p></td>
        </tr>
</table>

    <?php
}


// when the form gets submitted, and the category gets updated (in your case the option will get updated with the values of your custom fields above
add_filter(\'edited_terms\', \'update_my_category_fields\');
function update_my_category_fields($term_id) {
  if($_POST[\'taxonomy\'] == \'category\'):
    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
    $tag_extra_fields[$term_id][\'my_title\'] = strip_tags($_POST[\'_ce4-categoryTitle\']);
    $tag_extra_fields[$term_id][\'my_description\'] = strip_tags($_POST[\'_ce4_fullDescription\']);
    update_option(MY_CATEGORY_FIELDS, $tag_extra_fields);
  endif;
}


// when a category is removed
add_filter(\'deleted_term_taxonomy\', \'remove_my_category_fields\');
function remove_my_category_fields($term_id) {
  if($_POST[\'taxonomy\'] == \'category\'):
    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
    unset($tag_extra_fields[$term_id]);
    update_option(MY_CATEGORY_FIELDS, $tag_extra_fields);
  endif;
}
最后两个可能有一些与类别相关的挂钩,我没有搜索它们。我刚刚根据我的一个旧主题改编了上面的代码,在这个主题中,管理员可以将一个图像附加到自定义分类术语。。。

结束

相关推荐