Been using this answer -->> (link) 在我的类别中创建一些自定义字段,效果很好!我唯一的问题是,我有一个前端表单,允许用一个不相关的表单创建新类别,我想为我的自定义字段以及每个类别预定义字段插入值
我目前正在添加内置字段aka category name,slug&;说明
使用wp\\u insert\\u term使用此小支票(&B);插入脚本
if($_SERVER[\'REQUEST_METHOD\'] == "POST") {
$clientName = $_POST["clientName"];
$term_id = $_POST["clientName"];
$clientKeywords = $_POST["clientKeywords"];
$clientSlug = $_POST["clientSlug"];
$parent = \'\';
$errorType = \'Category "\'. $clientName .\'" Added!\';
wp_insert_term($clientName, \'category\', array(
\'description\'=>$clientKeywords,
\'slug\'=>sanitize_title($clientSlug),
\'parent\'=>$parent
));
}
Again.. inserting the custom fields values from the backend using the
code i have found in the answer i have linked from at the beginning of
my question works very well but i have tried a lot of ways to make it
work from the front-end / integrate it with wp_insert_term and have
not succeeded so far.任何帮助都将不胜感激
最合适的回答,由SO网友:Bainternet 整理而成
功能wp_insert_term
返回新创建的术语id(或WP\\u Error on Error),因此创建术语后,需要存储其id,然后可以使用get_option
, update_option
类似于:
if($_SERVER[\'REQUEST_METHOD\'] == "POST") {
$clientName = $_POST["clientName"];
$term_id = $_POST["clientName"];
$clientKeywords = $_POST["clientKeywords"];
$clientSlug = $_POST["clientSlug"];
$parent = \'\';
$errorType = \'Category "\'. $clientName .\'" Added!\';
$term_id = wp_insert_term($clientName, \'category\', array(
\'description\'=>$clientKeywords,
\'slug\'=>sanitize_title($clientSlug),
\'parent\'=>$parent
));
if (!is_wp_error($term_id)){
//save here
$tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
$tag_extra_fields[$term_id][\'field\'] = strip_tags($_POST[\'field\']);
$tag_extra_fields[$term_id][\'field2\'] = strip_tags($_POST[\'field2\']);
update_option(MY_CATEGORY_FIELDS, $tag_extra_fields);
}
}