WordPress如何添加创建自定义分类

前面我们说了WordPress如何添加创建自定义文章类型

创建了文章类型肯定还需要用到分类和标签,下面直接上代码

wordpress 创建自定义类型文章添加自定义分类

function create_movie_post_taxonomy()
{
	$labels = array(
		'name' => '分类',
		'singular_name' => 'country',
		'search_items' => '搜索',
		'popular_items' => '热门',
		'all_items' => '所有',
		'parent_item' => '父级分类',
		'parent_item_colon' => '父级分类',
		'edit_item' => '编辑',
		'update_item' => '更新',
		'add_new_item' => '添加',
		'new_item_name' => '名称',
		'separate_items_with_commas' => '按逗号分开',
		'add_or_remove_items' => '添加或删除',
		'choose_from_most_used' => '从经常使用的类型中选择',
		'menu_name' => '分类',
	);

	$args = array(
		'labels' => $labels,
		'hierarchical' => true,
		'description' => '小册创建',
		'public' => true,
		'exclude_from_search' =>false,
		'supports' => array( 'title', 'editor', 'category', 'thumbnail', 'excerpt', 'comments' ),
		'has_archive' => true,
		'query_var' => true,
	);
	register_taxonomy( 'movies', 'movie', $args );
}

add_action('init','create_movie_post_taxonomy');

wordpress 创建自定义类型文章添加自定义标签类型

function create_movie_post_tag() {
 
  $labels = array(
    'name' => _x( '标签', 'taxonomy general name' ),
    'singular_name' => _x( '标签', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Topics' ),
    'popular_items' => __( 'Popular Topics' ),
    'all_items' => __( 'All Topics' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( '编辑标签' ), 
    'update_item' => __( '更新标签' ),
    'add_new_item' => __( '新增标签' ),
    'new_item_name' => __( '标签名称' ),
    'separate_items_with_commas' => __( '多个标签请用英文逗号(,)分开' ),
    'add_or_remove_items' => __( '添加或删除标签' ),
    'choose_from_most_used' => __( '从常用标签中选择' ),
    'menu_name' => __( '标签' ),
  ); 
 
  register_taxonomy('movie_tag','movie',array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'movie_tag' ),
  ));
}
add_action( 'init', 'create_movie_post_tag', 0 );