将自定义字段添加到类别
-
-
可能重复[是否有向类别编辑器添加自定义字段的任何示例?](http://wordpress.stackexchange.com/questions/6549/any-examples-of-adding-custom-fields-to-the-category-editor)possible duplicate of [Any examples of adding custom fields to the category editor?](http://wordpress.stackexchange.com/questions/6549/any-examples-of-adding-custom-fields-to-the-category-editor)
- 0
- 2011-02-08
- Jan Fabry
-
这是我在执行此操作时使用的备忘单.它在短列表中有相关的动作挂钩和过滤器. http://www.charlestonsw.com/adding-custom-fields-to-the-wordpress-category-interface/Here is a cheat sheet I use when doing this. It has the relevant action hooks & filters in one short list. http://www.charlestonsw.com/adding-custom-fields-to-the-wordpress-category-interface/
- 0
- 2013-02-03
- Lance Cleveland
-
3 个回答
- 投票数
-
- 2016-06-29
从Wordpress 4.4开始, add_term_meta()和 update_term_meta()和get_term_meta()函数已添加.这意味着可以更新由MxmastaMills提供的代码,以使用少得多的黑客手段.
这是我的更新.我只想添加一个自定义标题,所以只有一个字段,但是对于所有要添加的字段,它的作用都相同.
function addTitleFieldToCat(){ $cat_title = get_term_meta($_POST['tag_ID'], '_pagetitle', true); ?> <tr class="form-field"> <th scope="row" valign="top"><label for="cat_page_title"><?php _e('Category Page Title'); ?></label></th> <td> <input type="text" name="cat_title" id="cat_title" value="<?php echo $cat_title ?>"><br /> <span class="description"><?php _e('Title for the Category '); ?></span> </td> </tr> <?php } add_action ( 'edit_category_form_fields', 'addTitleFieldToCat'); function saveCategoryFields() { if ( isset( $_POST['cat_title'] ) ) { update_term_meta($_POST['tag_ID'], '_pagetitle', $_POST['cat_title']); } } add_action ( 'edited_category', 'saveCategoryFields');
As of Wordpress 4.4, the add_term_meta(), the update_term_meta() and get_term_meta() functions have been added. This means that the code as provided by MxmastaMills can be updated to use a far less hacky approach.
Here is my update of it. There is only one field as I wanted to add a custom title, but it'll work the same for all the fields you want to add.
function addTitleFieldToCat(){ $cat_title = get_term_meta($_POST['tag_ID'], '_pagetitle', true); ?> <tr class="form-field"> <th scope="row" valign="top"><label for="cat_page_title"><?php _e('Category Page Title'); ?></label></th> <td> <input type="text" name="cat_title" id="cat_title" value="<?php echo $cat_title ?>"><br /> <span class="description"><?php _e('Title for the Category '); ?></span> </td> </tr> <?php } add_action ( 'edit_category_form_fields', 'addTitleFieldToCat'); function saveCategoryFields() { if ( isset( $_POST['cat_title'] ) ) { update_term_meta($_POST['tag_ID'], '_pagetitle', $_POST['cat_title']); } } add_action ( 'edited_category', 'saveCategoryFields');
-
没什么要注意的:在edited_category钩子中,tag_ID将在$ _POST数组中,而不在$ _GET中.同样," add_term_meta"实际上会添加一个新条目,而不是覆盖可能的旧条目.请改用`update_term_meta`.Few things to note: in the `edited_category` hook, `tag_ID` will be in the `$_POST` array, not in the `$_GET`. Also `add_term_meta` will actually add a new entry instead of overriding a possible old one. Use `update_term_meta` instead.
- 2
- 2016-10-02
- Martin Dimitrov
-
@MartinDimitrov您能通过单击"编辑"按钮来修正卢克-西蒙斯的答案吗?这样,每个人都可以使用可用的最佳代码,即使不是很好的代码(这里的设计师!).谢谢!@MartinDimitrov Could you fix luke-simmons's answer by clicking on edit button? This way everyone can use the best code available, even who does not code very well (designer here!). Thank you!
- 0
- 2016-11-08
- Hugo
-
它不会将数据保存在表单中It doesn't save the data in the form
- 1
- 2017-05-11
- Dev
-
@Dev确实会保存数据,除非您在第二行中将$ _POST更改为$ _GET,否则它不会显示.@Dev it does save data, it just don't show it unless you change $_POST to $_GET in second line.
- 0
- 2018-08-24
- banesto
-
- 2018-01-14
此代码有效:
add_action ( 'category_edit_form_fields', function( $tag ){ $cat_title = get_term_meta( $tag->term_id, '_pagetitle', true ); ?> <tr class='form-field'> <th scope='row'><label for='cat_page_title'><?php _e('Category Page Title'); ?></label></th> <td> <input type='text' name='cat_title' id='cat_title' value='<?php echo $cat_title ?>'> <p class='description'><?php _e('Title for the Category '); ?></p> </td> </tr> <?php }); add_action ( 'edited_category', function( $term_id ) { if ( isset( $_POST['cat_title'] ) ) update_term_meta( $term_id , '_pagetitle', $_POST['cat_title'] ); });
This code works:
add_action ( 'category_edit_form_fields', function( $tag ){ $cat_title = get_term_meta( $tag->term_id, '_pagetitle', true ); ?> <tr class='form-field'> <th scope='row'><label for='cat_page_title'><?php _e('Category Page Title'); ?></label></th> <td> <input type='text' name='cat_title' id='cat_title' value='<?php echo $cat_title ?>'> <p class='description'><?php _e('Title for the Category '); ?></p> </td> </tr> <?php }); add_action ( 'edited_category', function( $term_id ) { if ( isset( $_POST['cat_title'] ) ) update_term_meta( $term_id , '_pagetitle', $_POST['cat_title'] ); });
-
这不比另一个笨拙,我刚刚用WordPress 5.2.2进行了验证This is less clumsy than the other one and I just verified it with WordPress 5.2.2
- 0
- 2019-07-25
- nico gawenda
-
- 2011-02-07
Paul Menard在他的博客中提供了一个有关如何创建和使用术语meta的示例...
WordPress 3.0中新分类法的自定义元. .没有创建数据库表或检查设置
$_POST
vars的示例,因此您需要自己做这些小事情,但是看起来像是一个不错的代码库的...:)Paul Menard provided an example of how to create and use term meta in his blog...
Custom meta for new taxonomies in WordPress 3.0.There's no example of creating the DB table or checking
$_POST
vars are set, so you'll need to do those little things yourself, but it looks like a decent code base to build on top of ... :)
我想将自定义字段添加到特定类别. 一个类别仅包含以下字段:
名称:
插头:
父母:
说明:
由于我有电视连续剧网站,所以我想添加更多字段,当我创建新类别(类别=系列)时,我想要这样的内容
名称:
艺术家:
年份:
类型:
类型:
摘要:
插头:
父母:
说明:
依此类推...
请帮忙吗? 预先感谢.