wordpress 自定义post_type、分类、标签


function my_custom_post_item() {
  $labels = array(
    'name'               => _x( '商品', 'post type 名称' ),
    'singular_name'      => _x( '商品', 'post type 单个 item 时的名称,因为英文有复数' ),
    'add_new'            => _x( '新建商品', '添加新内容的链接名称' ),
    'add_new_item'       => __( '新建一个商品' ),
    'edit_item'          => __( '编辑商品' ),
    'new_item'           => __( '新商品' ),
    'all_items'          => __( '所有商品' ),
    'view_item'          => __( '查看商品' ),
    'search_items'       => __( '搜索商品' ),
    'not_found'          => __( '没有找到有关商品' ),
    'not_found_in_trash' => __( '回收站里面没有相关商品' ),
    'parent_item_colon'  => '',
    'menu_name'          => '商品'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => '网站的商品信息',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' , 'page-attributes', 'custom-fields'),
    'has_archive'   => true,
	'taxonomies'            => array( 'category', 'post_tag' ),
	//'taxonomies'            => array(  'post_tag' ),
	'show_ui'            => true,
	'query_var'          => true,
	'hierarchical' => false,
	'show_in_nav_menus'  => true,
		'show_in_rest'=>true,
  );
  register_post_type( 'item', $args );
}
add_action( 'init', 'my_custom_post_item' );


function my_taxonomies_item() {
  $labels = array(
    'name'              => _x( '商品分类', 'taxonomy 名称' ),
    'singular_name'     => _x( '商品分类', 'taxonomy 单数名称' ),
    'search_items'      => __( '搜索商品分类' ),
    'all_items'         => __( '所有商品分类' ),
    'parent_item'       => __( '该商品分类的上级分类' ),
    'parent_item_colon' => __( '该商品分类的上级分类:' ),
    'edit_item'         => __( '编辑商品分类' ),
    'update_item'       => __( '更新商品分类' ),
    'add_new_item'      => __( '添加新的商品分类' ),
    'new_item_name'     => __( '新商品分类' ),
    'menu_name'         => __( '商品分类' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
	'show_ui' => true,   
	'query_var' => true,  
	'rewrite' => array( 'slug' => 'fenlei' ),
	'show_admin_column' => true,

  );
  register_taxonomy( 'fenlei', 'item', $args );
}
add_action( 'init', 'my_taxonomies_item', 0 );





//*****


add_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 );
function create_topics_nonhierarchical_taxonomy() {
 
  $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' => __( 'Separate topics with commas' ),
    'add_or_remove_items' => __( 'Add or remove topics' ),
    'choose_from_most_used' => __( 'Choose from the most used topics' ),
    'menu_name' => __( '商品标签' ),
  ); 
 
  register_taxonomy('item_tag','item',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' => 'item_tag' ),
  ));
}





//*****
//自定义固定链接
add_filter('post_type_link', 'custom_book_link', 1, 3);
function custom_book_link( $link, $post = 0 ){
	//echo '###' . $post->post_type;
	if( $post->post_type == 'item' ){
		return home_url( 'item/' . $post->ID .'.html' );
	}else{
		return $link;
	}
}
add_action( 'init', 'custom_book_rewrites_init' );
function custom_book_rewrites_init(){
	add_rewrite_rule( 'item/([0-9]+)?.html$', 'index.php?post_type=item&p=$matches[1]', 'top');
	add_rewrite_rule( 'article/([0-9]+)?.html$', 'index.php?post_type=article&p=$matches[1]', 'top');
}


//********************

function custom_page_rules() {
    global $wp_rewrite;
    //修改page的固定链接结构
    $wp_rewrite->page_structure = $wp_rewrite->root . '/%pagename%.html'; 
	
	//修改分类的固定链接结构
    $wp_rewrite->extra_permastructs['fenlei']['with_front'] = '';
    $wp_rewrite->extra_permastructs['fenlei']['struct'] = 'fenlei/%fenlei%';
 
    //修改tag的固定链接结构
    $wp_rewrite->extra_permastructs['item_tag']['with_front'] = '';
    $wp_rewrite->extra_permastructs['item_tag']['struct'] = 'item_tag/%item_tag%';
	
}
add_action( 'init', 'custom_page_rules' );

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值