我是否可以向特定的分类法添加一些自定义字段,以便在调用该分类法时,也应该显示附加到该分类法的自定义字段。
Custom fields to taxonomy
2 个回复
最合适的回答,由SO网友:Kevin Langley Jr. 整理而成
您可以钩住该特定分类法的编辑表单字段操作。
如果您查看/wp admin/edit标签表单。php每个分类法都有挂钩。从第69行开始:
if ( \'category\' == $taxonomy )
do_action(\'edit_category_form_fields\', $tag);
elseif ( \'link_category\' == $taxonomy )
do_action(\'edit_link_category_form_fields\', $tag);
else
do_action(\'edit_tag_form_fields\', $tag);
do_action($taxonomy . \'_edit_form_fields\', $tag, $taxonomy);
因此,对于自定义分类法,应该是$taxonomy\\u edit\\u form\\u字段,其中$taxonomy是分类法名称。因此,您可以在函数中添加钩子,添加表单字段。然后,您还需要挂接“edit\\u term”操作,如果表单字段不为空,请使用update\\u option()将其另存为选项。这不允许您仅“调用分类法”并将它们附加到对象,但您可以在获得术语后循环使用它们,并很容易获得与该术语相关联的选项。
SO网友:Yoav Kadosh
我开发了一个小脚本,简化了将自定义字段添加到分类法(核心和自定义)的过程,并允许您为每个字段向terms表添加列。脚本被调用amarkal-taxonomy, 并且是Amarkal WordPress框架。
使用amarkal-taxonomy
, 添加自定义字段可简化为:
// Add a text field to the \'category\' taxonomy \'add\' & \'edit\' forms:
amarkal_taxonomy_add_field(\'category\', \'cat_icon\', array(
\'type\' => \'text\',
\'label\' => \'Icon\',
\'description\' => \'The category\\\'s icon\',
\'table\' => array(
\'show\' => true, // Add a column to the terms table
\'sortable\' => true // Make that column sortable
)
));
// Then you can retrieve the data using:
$icon = get_term_meta( $term_id, \'cat_icon\', true );
你可以改变\'category\'
无论你的分类名称是什么。结束