自定义分类在后保存时创建不需要的整型术语

时间:2017-07-14 作者:George Edward Shaw IV

在我选择术语并保存我的自定义帖子类型后,将使用整数值生成新的分类术语,并且每次都会选择这些术语。我不知道问题是什么,也没有在互联网上发现任何类似的问题。下面是一张具体情况的图片:http://prntscr.com/fvfqvf

注册职位类型

function tbs_register_post_type() {
    $singular = \'Stylist\';
    $plural = \'Stylists\';

    $labels = array(
        \'name\' => $plural,
        \'singular_name\' => $singular,
        \'add_new\' => \'Add New\',
        \'add_new_item\' => \'Add New \' . $singular,
        \'edit_item\' => \'Edit \' . $singular,
        \'new_item\' => \'New \' . $singular,
        \'view_item\' => \'View \' . $singular,
        \'view_items\' => \'View \' . $plural,
        \'search_items\' => \'Search \' . $plural,
        \'not_found\' => \'No \' . strtolower($plural) . \' found\',
        \'not_found_in_trash\' => \'No \' . strtolower($plural) . \' found in trash\',
        \'all_items\' => \'All \' . $plural,
        \'archives\' => $singular . \' Archives\',
        \'attributes\' => $singular . \' Attributes\',
        \'insert_into_item\' => \'Insert into \' . strtolower($singular),
        \'uploaded_to_this_item\' => \'Uploaded to this \' . strtolower($singular),
        \'featured_image\' => $singular . \' Picture\',
        \'set_featured_image\' => \'Set \' . strtolower($singular) . \' picture\',
        \'remove_featured_image\' => \'Remove \' . strtolower($singular) . \' picture\',
        \'use_featured_image\' => \'Use as \' . strtolower($singular) . \' picture\',
        \'filter_items_list\' => \'Filter \' . strtolower($plural) . \' list\',
        \'items_list_navigation\' => $plural . \' list navigation\',
        \'items_list\' => $plural . \' list\'
    );

    $args = array(
        \'label\' => $plural,
        \'labels\' => $labels,
        \'description\' => \'A custom plugin to create, edit, and delete stylists made for -.\',
        \'public\' => true,
        \'menu_position\' => \'2\',
        \'menu_icon\' => \'dashicons-groups\',
        \'supports\' => array(\'title\', \'thumbnail\'),
        \'taxonomies\' => array(\'tbs_skills\', \'tbs_fun_facts\'),
        \'has_archive\' => false,
        \'rewrite\' => array(\'slug\' => \'stylists\')
    );

    register_post_type(\'tbs_stylists\', $args);
}
add_action(\'init\', \'tbs_register_post_type\');
注册分类法

function tbs_register_skills_taxonomy() {
    $singular = \'Skill\';
    $plural = \'Skills\';

    $labels = array(
        \'name\' => $plural,
        \'singular_name\' => $singular,
        \'all_items\' => _(\'All \'. $plural),
        \'edit_item\' => _(\'Edit \' . $singular),
        \'view_item\' => _(\'View \' . $singular),
        \'update_item\' => _(\'Update \' . $singular),
        \'add_new_item\' => _(\'Add New \' . $singular),
        \'new_item_name\' => _(\'New \' . $singular . \' Name\'),
        \'search_items\' => _(\'Search \' . $plural),
        \'popular_items\' => _(\'Popular \' . $plural),
        \'separate_items_with_commas\' => _(\'Separate \' . strtolower($plural) . \' with commas\'),
        \'add_or_remove_items\' => _(\'Add or remove \' . strtolower($plural)),
        \'choose_from_most_used\' => _(\'Choose from the most common \' . strtolower($plural)),
        \'not_found\' => _(\'No \' . strtolower($plural) . \' found.\')
    );

    $args = array(
        \'label\' => $plural,
        \'labels\' => $labels,
        \'description\' => \'A custom taxonomy for the stylists custom post type made for -.\',
        \'public\' => true
    );

    register_taxonomy(\'tbs_skills\', \'tbs_stylists\', $args);
}
add_action(\'init\', \'tbs_register_skills_taxonomy\');

function tbs_register_fun_facts_taxonomy() {
    $singular = \'Fun Fact\';
    $plural = \'Fun Facts\';

    $labels = array(
        \'name\' => $plural,
        \'singular_name\' => $singular,
        \'all_items\' => _(\'All \'. $plural),
        \'edit_item\' => _(\'Edit \' . $singular),
        \'view_item\' => _(\'View \' . $singular),
        \'update_item\' => _(\'Update \' . $singular),
        \'add_new_item\' => _(\'Add New \' . $singular),
        \'new_item_name\' => _(\'New \' . $singular . \' Name\'),
        \'search_items\' => _(\'Search \' . $plural),
        \'popular_items\' => _(\'Popular \' . $plural),
        \'separate_items_with_commas\' => _(\'Separate \' . strtolower($plural) . \' with commas\'),
        \'add_or_remove_items\' => _(\'Add or remove \' . strtolower($plural)),
        \'choose_from_most_used\' => _(\'Choose from the most common \' . strtolower($plural)),
        \'not_found\' => _(\'No \' . strtolower($plural) . \' found.\')
    );

    $args = array(
        \'label\' => $plural,
        \'labels\' => $labels,
        \'description\' => \'A custom taxonomy for the stylists custom post type made for -.\',
        \'public\' => true
    );

    register_taxonomy(\'tbs_fun_facts\', \'tbs_stylists\', $args);
}
add_action(\'init\', \'tbs_register_fun_facts_taxonomy\');
编辑帖子页面功能

function tbs_change_title_text($title){
    $screen = get_current_screen();
    if (\'tbs_stylists\' == $screen->post_type) $title = \'Enter stylist name\';
    return $title;
}
add_filter(\'enter_title_here\', \'tbs_change_title_text\');

function tbs_move_metas() {
    remove_meta_box(\'tagsdiv-tbs_skills\', \'tbs_stylists\', \'side\');
    add_meta_box(\'tagsdiv-tbs_skills\', \'Skills\', \'post_categories_meta_box\', \'tbs_stylists\', \'normal\', \'high\', array(\'taxonomy\' => \'tbs_skills\'));

    remove_meta_box(\'tagsdiv-tbs_fun_facts\', \'tbs_stylists\', \'side\');
    add_meta_box(\'tagsdiv-tbs_fun_facts\', \'Fun Facts\', \'post_categories_meta_box\', \'tbs_stylists\', \'normal\', \'high\', array(\'taxonomy\' => \'tbs_fun_facts\'));
}
add_action(\'admin_init\', \'tbs_move_metas\');
如果有人能提供一些见解,我将不胜感激。非常感谢。

1 个回复
SO网友:nimmolo

这似乎是由于声明非层次化自定义分类法与使用post\\u categories\\u meta\\u框(复选框)作为接口之间存在冲突。

您可以将这种差异称为WP-Core中的一个bug,这当然是因为分类法声明本身允许冲突。

问题似乎是,对于非层次分类法,复选框以输入“value”=term\\u id呈现,但在保存后使用的复选框回调将选中项的值存储为术语名称,因此您可以将该整数(id)创建并存储为“新”术语。

另请参见this question

目前的解决方案:如果要使用checkbox接口,只需将分类声明为层次结构。

结束

相关推荐

Custom taxonomy Rewrite Rule

我希望我的分类url如下:site。com/page/taxonomy\\u术语我如何才能做到这一点?我在函数中编写了wordpress重写规则代码。php如下所示:function sphere_custom_rewrite_rules( $rules ){ $newrules[\'(.+?)/([^/]*)/?\'] = \'index.php?pagename=$matches[1]&positive_sphere=$matches[2]\'; $newrul