欢迎光临
我们一直在努力

WordPress 主题添加影视分类

最近给自己的博客添加一个影视类型点击查看效果

下面记录所需要的代码

首先需要在主题的“functions.php”添加一个函数然后使用“add_action()”添加自定义分类

function cwtk_movie() {
	$labels = array(
		'name'                  => '影视',
		'singular_name'         => '影视',
		'menu_name'             => '影视',
		'name_admin_bar'        => '影视',
		'archives'              => '影视',
		'parent_item_colon'     => '父分类',
		'all_items'             => '所有影视',
		'add_new_item'          => '添加影视',
		'add_new'               => '添加影视',
		'new_item'              => '新影视',
		'edit_item'             => '编辑影视',
		'update_item'           => '更新影视',
		'view_item'             => '查看影视',
		'search_items'          => '搜索影视',
		'not_found'             => '没有影视',
		'not_found_in_trash'    => '回收站内没有影视',
		'featured_image'        => '海报图片',
		'set_featured_image'    => '设置海报图片',
		'remove_featured_image' => '移除海报图片',
		'use_featured_image'    => '设为海报图片',
		'insert_into_item'      => '插入影视',
		'uploaded_to_this_item' => '上传',
		'items_list'            => '影视列表',
		'items_list_navigation' => '影视列表导航',
		'filter_items_list'     => '筛选影视列表',
	);
	$args = array(
		'labels' => $labels,
	  'label'               => '影视',
		'description'         => '影视功能',
		'public' 				      => true,
		'publicly_queryable' 	=> true,
		'show_ui' 				    => true,
		'show_in_menu' 			  => true,
		'query_var' 			    => true,
		'rewrite' 				    => true,
		'capability_type' 		=> 'post',
		'has_archive' 			  => true,
		'hierarchical' 			  => false,
		'menu_position' 		  => 4,
	  'menu_icon'           => 'dashicons-format-video',
		//'taxonomies'=> array('category'),
    //'taxonomies'          => array('post_tag'),
		'supports' 				    => array('title','editor','author','thumbnail','comments')
	);
}
add_action('init', 'cwtk_movie');

接下来我们还需要其他的分类如:类型,地区,演员,年份。需要在函数里继续添加代码:

function cwtk_movie() {
	$labels = array(
		'name'                  => '影视',
		'singular_name'         => '影视',
		'menu_name'             => '影视',
		'name_admin_bar'        => '影视',
		'archives'              => '影视',
		'parent_item_colon'     => '父分类',
		'all_items'             => '所有影视',
		'add_new_item'          => '添加影视',
		'add_new'               => '添加影视',
		'new_item'              => '新影视',
		'edit_item'             => '编辑影视',
		'update_item'           => '更新影视',
		'view_item'             => '查看影视',
		'search_items'          => '搜索影视',
		'not_found'             => '没有影视',
		'not_found_in_trash'    => '回收站内没有影视',
		'featured_image'        => '海报图片',
		'set_featured_image'    => '设置海报图片',
		'remove_featured_image' => '移除海报图片',
		'use_featured_image'    => '设为海报图片',
		'insert_into_item'      => '插入影视',
		'uploaded_to_this_item' => '上传',
		'items_list'            => '影视列表',
		'items_list_navigation' => '影视列表导航',
		'filter_items_list'     => '筛选影视列表',
	);
	$args = array(
		'labels' => $labels,
	  'label'               => '影视',
		'description'         => '影视功能',
		'public' 				      => true,
		'publicly_queryable' 	=> true,
		'show_ui' 				    => true,
		'show_in_menu' 			  => true,
		'query_var' 			    => true,
		'rewrite' 				    => true,
		'capability_type' 		=> 'post',
		'has_archive' 			  => true,
		'hierarchical' 			  => false,
		'menu_position' 		  => 4,
	  'menu_icon'           => 'dashicons-format-video',
		//'taxonomies'=> array('category'),
    //'taxonomies'          => array('post_tag'),
		'supports' 				    => array('title','editor','author','thumbnail','comments')
	);
	register_post_type('movie',$args);
	//注册类型
	$labels = array(
		'name'              => '类型',
		'singular_name'     => '类型',
		'search_items'      => '搜索类型',
		'all_items'         => '所有类型',
		'parent_item'       => '该类型的上级',
		'parent_item_colon' => '该类型的上级:',
		'edit_item'         => '编辑类型',
		'update_item'       => '更新类型',
		'add_new_item'      => '添加类型',
		'new_item_name'     => '新类型',
		'menu_name'         => '类型',
		);
	$rewrite = array(
		'slug' => 'type'
		);
	$args = array(
    'hierarchical' => true,
    'labels' 		   => $labels,
    'show_ui' 		 => true,
    'query_var' 	 => true,
    'rewrite' 		 => $rewrite
	);
  register_taxonomy('movietype', 'movie', $args);
	//注册地区
	$labels = array(
		'name'              => '地区',
		'singular_name'     => '地区',
		'search_items'      => '搜索地区',
		'all_items'         => '所有地区',
		'parent_item'       => '该地区的上级',
		'parent_item_colon' => '该地区的上级:',
		'edit_item'         => '编辑地区',
		'update_item'       => '更新地区',
		'add_new_item'      => '添加地区',
		'new_item_name'     => '新地区',
		'menu_name'         => '地区',
		);
	$rewrite = array(
		'slug' => 'country'
		);
	$args = array(
    'hierarchical' => true,
    'labels' 		   => $labels,
    'show_ui' 		 => true,
    'query_var' 	 => true,
    'rewrite' 		 => $rewrite
	);
  register_taxonomy('moviecountry', 'movie', $args);
	//注册演员
	$labels = array(
		'name'          => '演员',
		'singular_name' => '演员',
		'search_items'  => '搜索演员',
		'all_items'     => '所有演员',
		'edit_item'     => '编辑演员',
		'update_item'   => '更新演员',
		'add_new_item'  => '添加演员',
		'new_item_name' => '新演员',
		'menu_name'     => '演员',
		);
	$rewrite = array(
		'slug' => 'actor'
		);
	$args = array(
    'labels' 		=> $labels,
    'show_ui' 	=> true,
    'query_var' => true,
    'rewrite' 	=> $rewrite
	);
  register_taxonomy('movieactor', 'movie', $args);
  //注册年份
  $labels = array(
    'name'          => '年份',
    'singular_name' => '年份',
    'search_items'  => '搜索年份',
    'all_items'     => '所有年份',
    'edit_item'     => '编辑年份',
    'update_item'   => '更新年份',
    'add_new_item'  => '添加年份',
    'new_item_name' => '新年份',
    'menu_name'     => '年份',
  );
  $rewrite = array(
    'slug' => 'year'
    );
  $args = array(
		'hierarchical' => true,
    'labels' 			 => $labels,
    'show_ui' 		 => true,
    'query_var' 	 => true,
    'rewrite' 		 => $rewrite
  );
  register_taxonomy('movieyear', 'movie', $args);
	//注册标签
	$labels = array(
		'name'              => '标签',
		'singular_name'     => '标签',
		'search_items'      => '搜索标签',
		'all_items'         => '所有标签',
		'edit_item'         => '编辑标签',
		'update_item'       => '更新标签',
		'add_new_item'      => '添加标签',
		'new_item_name'     => '新标签',
		'menu_name'         => '标签',
	);
	$rewrite = array(
		'slug' => 'mtag'
	);
	$args = array(
		'labels' 		=> $labels,
		'show_ui' 		=> true,
		'query_var' 	=> true,
		'rewrite' 		=> $rewrite
	);
	register_taxonomy('movietag', 'movie', $args);
}
add_action('init', 'cwtk_movie');

添加完保存就可以在后台看到我们添加的影视功能了。

赞(0) 打赏
未经允许不得转载:NixonLi博客 » WordPress 主题添加影视分类

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏