进行了此自定义分类法,可以控制单个帖子的布局,基本上我想添加两个选项:布局1和布局2。
// Register Custom Taxonomy
function custom_taxonomy() {
$labels = array(
'name' => _x( 'Layout', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Layout', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Taxonomy', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'New Item Name', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
);
$capabilities = array(
'manage_terms' => 'foobar',
'edit_terms' => 'foobar',
'delete_terms' => 'foobar',
'assign_terms' => 'foobar'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'capabilities' => $capabilities,
);
register_taxonomy( 'Layout', array( 'post' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'custom_taxonomy', 0 );
功能设置为无效值以阻止术语被编辑,但是wp_insert_term无法正常工作。
在我的自定义分类法中添加两个术语不会那么难,对吧?
尽管将我的自定义分类名称更改为“ category”(即WP默认),但使用它插入术语是行不通的。是什么赋予了?
function example_insert_category() {
$parent_term = term_exists( 'layout' ); // array is returned if taxonomy is given
$parent_term_id = $parent_term['term_id']; // get numeric term id
wp_insert_term(
'Example Category',
'layout',
array(
'description' => 'This is an example category created with wp_insert_term.',
'slug' => 'example-category'
)
);
}
add_action( 'after_setup_theme', 'example_insert_category' );