0

在创建分类法时,我正在尝试务实地添加分类法术语。我尝试添加

wp_insert_term(
    'A00',   // the term 
    'we_colors'
);

tax_color_palletes但这不是添加术语A00。我正在处理这个片段;我怎样才能解决这个问题?

function tax_color_palletes() {

    $labels = array(
        'name'                       => 'Models',
        'singular_name'              => 'Model',
        'menu_name'                  => 'Models',
        'all_items'                  => 'All Models',
        'parent_item'                => 'Parent Model',
        'parent_item_colon'          => 'Parent Model',
        'new_item_name'              => 'Model Name',
        'add_new_item'               => 'Add New Model',
        'edit_item'                  => 'Edit Model',
        'update_item'                => 'Update Model Type',
        'separate_items_with_commas' => 'Separate Model with commas',
        'search_items'               => 'Search Models',
        'add_or_remove_items'        => 'Add or remove Model Type',
        'choose_from_most_used'      => 'Choose from the most used Model',
        'not_found'                  => 'Model Not Found',
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'women_models', array( 'we_colors' ), $args );
    wp_insert_term(
        'A00',   // the term 
        'we_colors'
    );
}
add_action( 'init', 'tax_color_palletes', 0 );
4

1 回答 1

0

您是否要添加term到您的taxonomyor中post_type

在您的示例中,您将“women_models”注册taxonomypost_type“we_colors”。但是随后您调用wp_insert_term(需要 a taxonomy)并将其传递给post_type. 这应该给你一个错误。

如果您只想将 a 添加term到 a taxonomy,则需要将您的 to 传递taxonomywp_insert_term.

wp_insert_term('A00', 'women_models');

相反,如果您实际上是在尝试将术语添加到 post_type,则应该使用wp_set_object_termswhich can 反过来调用wp_insert_term自身来创建新术语。但是,您将需要获得第$object_id一个。

wp_set_object_terms( $object_id, ['term_1', 'term_2'], 'taxonomy_name');

https://developer.wordpress.org/reference/functions/register_taxonomy/ https://developer.wordpress.org/reference/functions/wp_insert_term/ https://developer.wordpress.org/reference/functions/wp_set_object_terms/

于 2018-09-04T22:52:35.647 回答