前端更新自定义类别字段

时间:2012-04-15 作者:Sagive

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.

任何帮助都将不胜感激

1 个回复
最合适的回答,由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);
    }
}

结束

相关推荐

WP_LIST_CATEGORIES()排除除一个类别外的所有类别

有没有办法排除除一个类别之外的所有类别?我想显示一个类别和它的子类别作为下拉菜单,但管理员可能会添加更多的子类别,所以我不想限制他们可以放在那里的唯一ID。所以我想排除除1及其子类别之外的所有类别。wp\\u list\\u categories()是否可以这样做?