在类别编辑器中添加自定义字段的任何示例吗?
1 个回答
- 投票数
-
- 2011-01-08
不.您必须使用
wp_options
,因为您无法在wp_term_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; }
最后两个可能有一些与类别相关的挂钩,我没有进行搜索.我只是根据我的一个旧主题改编了上面的代码,在该主题中,管理员可以将图像附加到自定义分类法术语中.
No. You have to use
wp_options
, because you can't create new fields in the wp_term_taxonomy table (If you do, in the next WP update you'll loose them).So:
// 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; }
There might be some category-related hooks for the last two, I didn't search for them. I just adapted the code above from a old theme of mine in which the admin can attach a image to a custom taxonomy term...
-
@AA:谢谢,这很好!查看类别存档页面时,如何检索标题的值?@AA: Thanks, this works great! How would you retrieve the value for the title when you are viewing the category archive page?
- 0
- 2011-01-11
- Scott B
-
使用`get_option(MY_CATEGORY_FIELDS)`using `get_option(MY_CATEGORY_FIELDS)`
- 0
- 2011-01-11
- onetrickpony
-
我使用了这段代码,效果很好.我发现我要添加的字段被放置在添加类别按钮下方.您是否知道一种使新字段显示在按钮上方的方法?我试图从代码中删除表,只是离开了tr和td,但是那没用I used this code and it works great. I found that the field I want to add gets put below the add category button. Do you know a way to get the new fields to show above the button? I tried removing the table from the code and just left the tr and td but that didn't work
- 0
- 2012-09-25
- Jamie
-
是的,显然WP现在有新的操作可供您使用.主要是`edit_category_form_fields`而不是`edit_category_form`.参见[edit_tags.php](http://core.svn.wordpress.org/trunk/wp-admin/edit-tags.php)yes, apparently WP has new actions now which you can use. Mainly `edit_category_form_fields` instead of `edit_category_form`. See [edit_tags.php](http://core.svn.wordpress.org/trunk/wp-admin/edit-tags.php)
- 0
- 2012-09-25
- onetrickpony
-
使用add_action(
." _ add_form_fields",my_action)扩展类别表单编辑器.即在对象上下文中,针对您的自定义操作的add_action(" store_add_form_fields",array($this,'add_form_fields))会与"存储"的分类类型挂钩.WordPress的3.X +版本更喜欢将 格式用于动作挂钩,较旧的样式"edit_category_form_fields"以及其他样式在代码中标记为"旧版支持",这意味着它们最终可能会消失..参考:http://core.trac.wordpress.org/browser/tags/3.4.2/wp-admin/edit-tags.php Use add_action(."_add_form_fields",my_action) to extend the category form editor. i.e. add_action("store_add_form_fields",array($this,'add_form_fields)) in an object context for your custom action hooks for a taxonomy type of 'store'. Version 3.X+ of WordPress prefers the format for action hooks, the older style of "edit_category_form_fields" and others are marked as "legacy support" in the code, which means they may/will eventually go away. ref: http://core.trac.wordpress.org/browser/tags/3.4.2/wp-admin/edit-tags.php - 0
- 2012-10-03
- Lance Cleveland
我想我快要破了这个螺母:)
我正在尝试向"类别"编辑器添加一组自定义字段.由于我不处理postmeta,因此我相信我会将自定义类别字段值写入wp_term_taxonomy表而不是wp_options表.这是正确的吗?
如果有任何执行此操作的示例,请共享链接或部分代码.我不确定如何捕获和保存我的自定义类别字段.
这是我的代码...