自定义字段到类别编辑页面,然后在模板中正确显示

时间:2011-03-08 作者:daveaspinall

最近似乎在这个项目上遇到了一些问题,所以很抱歉向您发送垃圾邮件!我需要在admin中为category edit页面提供一些附加字段,以便我的客户端更好地管理它们。

我添加的额外字段是“Category Features”和“Category Applications”,下面的代码似乎按预期工作:

// Add custom fields to category edit pages
// the option name
define(\'Category_Extras\', \'Category_Extras_option\');

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



    ?>

<table class="form-table">
        <tr class="form-field">
            <th scope="row" valign="top"><label for="Category_Extras_Features">Category Features</label></th>
            <td><textarea style="width: 97%;" cols="50" rows="5" name="Category_Extras_Features" id="Category_Extras_Features" class="tinymce"><?php  echo $tag_extra_fields[$tag->term_id][\'Category_Extras_Features\']; ?></textarea>
            <p class="description">This isn\'t applicable to all categories but allows additional information to be displayed on a category page.</p></td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="Category_Extras_Applications">Category Applications</label></th>
            <td><textarea style="width: 97%;" cols="50" rows="5" name="Category_Extras_Applications" id="Category_Extras_Applications" class="Category_Extras_Features"><?php  echo $tag_extra_fields[$tag->term_id][\'Category_Extras_Applications\']; ?></textarea>
            <p class="description">This isn\'t applicable to all categories but allows additional information to be displayed on a category page.</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_Category_Extras\');
function update_Category_Extras($term_id) {
  if($_POST[\'taxonomy\'] == \'category\'):
    $tag_extra_fields = get_option(Category_Extras);
    $tag_extra_fields[$term_id][\'Category_Extras_Features\'] = strip_tags($_POST[\'Category_Extras_Features\']);
    $tag_extra_fields[$term_id][\'Category_Extras_Applications\'] = strip_tags($_POST[\'Category_Extras_Applications\']);
    update_option(Category_Extras, $tag_extra_fields);
  endif;
}


// when a category is removed
add_filter(\'deleted_term_taxonomy\', \'remove_Category_Extras\');
function remove_Category_Extras($term_id) {
  if($_POST[\'taxonomy\'] == \'category\'):
    $tag_extra_fields = get_option(Category_Extras);
    unset($tag_extra_fields[$term_id]);
    update_option(Category_Extras, $tag_extra_fields);
  endif;
}
我在页面上回应如下:

<?php $prod_features = get_option(Category_Extras); foreach($prod_features as $feature) { echo $feature[\'Category_Extras_Features\']; } ?>
然而,很明显,它呼应了所有类别的所有选项。是否有其他功能允许我呼应当前类别的“类别特征”?(此代码显示在category.php页面上)

一如既往,我非常感谢您的帮助:-)

谢谢

戴夫

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

我自己整理的!可能会帮助一些人。。。

$cat = get_query_var(\'cat\');
$prod_extras = get_option(Category_Extras);
$this_application = $prod_extras[$cat][\'Category_Extras_Applications\'];

if(!empty($this_application)) { echo $this_application; }
干杯,

戴夫

SO网友:avs

对于类别自定义字段,有以下插件:http://wordpress.org/extend/plugins/categorycustomfields/

结束

相关推荐

为什么我有<?php wp_list_Categories>中的流弹?

在这个页面上,我在搜索工作导航的右侧有一个流弹?为什么会这样?也许我错过了一些简单的事情。我只是呆了一会儿。http://shs.merms.info/2010/12/30/shs-team-celebrates-successful-quarter/<ul> <li><?php wp_list_categories(\'title_li=<strong>Categories</strong><br />\'); ?>