不能用程序插入术语


not able to insert term programatically

我正在尝试使用代码添加术语,但它没有添加到wordpress中。我正在使用以下代码添加

$term = wp_insert_term('Jogger', 'product_style',0);

每次返回Invalid taxonomy。我已经检查了我的数据库,没有找到任何条目。即使我已经用term_exists('Jogger', 'pa_style',0);进行了检查,它也返回0。有人知道吗?

我可以添加其他术语,如

$term = wp_insert_term('SPORTS', 'product_cat',0);

在functions.php 中使用此代码

        add_action( 'init', 'create_new_taxonomy' );
        function create_new_taxonomy() {
            register_taxonomy(
                'product_style',
                'products',
                array(
                    'label' => __( 'Product Style' ),
                    'rewrite' => array( 'slug' => 'product_style' ),
                    'hierarchical' => true,
                )
            );
        }

您不能在此处插入术语,因为您的分类法尚未注册。所以使用上面的代码先注册它。然后插入术语。