php 创建具有自定义分类的自定义帖子类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 创建具有自定义分类的自定义帖子类型相关的知识,希望对你有一定的参考价值。

function register_custom_post_types() {	

	/**
	 * Array of Service Labels for register_post_type
	 *
	 * @var array $service_labels
	 */
	$service_labels = array(
		'name'                  => _x( 'Services', 'Post Type General Name', 'textdomain-cpts' ),
		'singular_name'         => _x( 'Service', 'Post Type Singular Name', 'textdomain-cpts' ),
		'menu_name'             => __( 'Services', 'textdomain-cpts' ),
		'name_admin_bar'        => __( 'Service', 'textdomain-cpts' ),
		'archives'              => __( 'Service Archives', 'textdomain-cpts' ),
		'parent_item_colon'     => __( 'Parent Service:', 'textdomain-cpts' ),
		'all_items'             => __( 'All Services', 'textdomain-cpts' ),
		'add_new_item'          => __( 'Add New Service', 'textdomain-cpts' ),
		'add_new'               => __( 'Add New', 'textdomain-cpts' ),
		'new_item'              => __( 'New Service', 'textdomain-cpts' ),
		'edit_item'             => __( 'Edit Service', 'textdomain-cpts' ),
		'update_item'           => __( 'Update Service', 'textdomain-cpts' ),
		'view_item'             => __( 'View Service', 'textdomain-cpts' ),
		'search_items'          => __( 'Search Service', 'textdomain-cpts' ),
		'not_found'             => __( 'Not found', 'textdomain-cpts' ),
		'not_found_in_trash'    => __( 'Not found in Trash', 'textdomain-cpts' ),
		'featured_image'        => __( 'Featured Image', 'textdomain-cpts' ),
		'set_featured_image'    => __( 'Set featured image', 'textdomain-cpts' ),
		'remove_featured_image' => __( 'Remove featured image', 'textdomain-cpts' ),
		'use_featured_image'    => __( 'Use as featured image', 'textdomain-cpts' ),
		'insert_into_item'      => __( 'Insert into service', 'textdomain-cpts' ),
		'uploaded_to_this_item' => __( 'Uploaded to this item', 'textdomain-cpts' ),
		'items_list'            => __( 'Services list', 'textdomain-cpts' ),
		'items_list_navigation' => __( 'Services list navigation', 'textdomain-cpts' ),
		'filter_items_list'     => __( 'Filter Services list', 'textdomain-cpts' ),
	);

	/**
	 * An array of service arguments for register_post_type
	 *
	 * @var array $service_args
	 */
	$service_args = array(
		'label'               => __( 'Service', 'textdomain-cpts' ),
		'labels'              => $service_labels,
		'supports'            => array(
			'title',
			'editor',
			'thumbnail',
			'excerpt',
			'custom-fields',
			'page-attributes',
			'revisions',
		),
		'hierarchical'        => true,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'menu_position'       => 30,
		'menu_icon'           => 'dashicons-format-chat',
		'show_in_admin_bar'   => true,
		'show_in_nav_menus'   => true,
		'can_export'          => true,
		'taxonomies'          => array(
			'product_line',
			'product_color',
		),
		'has_archive'         => false,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
	);

	register_post_type( 'service', $service_args );
	
	/**
	 * Taxonomy for Service CPT
	 *
	 * @var array $service_type_labels
	 */
	$service_type_labels = array(
		'name'                       => _x( 'Service Types', 'Taxonomy General Name', 'textdomain-cpts' ),
		'singular_name'              => _x( 'Service Type', 'Taxonomy Singular Name', 'textdomain-cpts' ),
		'menu_name'                  => __( 'Service Type', 'textdomain-cpts' ),
		'all_items'                  => __( 'All Service Types', 'textdomain-cpts' ),
		'parent_item'                => __( 'Parent Service Type', 'textdomain-cpts' ),
		'parent_item_colon'          => __( 'Parent Service Type:', 'textdomain-cpts' ),
		'new_item_name'              => __( 'New Service Type Name', 'textdomain-cpts' ),
		'add_new_item'               => __( 'Add New Service Type', 'textdomain-cpts' ),
		'edit_item'                  => __( 'Edit Service Type', 'textdomain-cpts' ),
		'update_item'                => __( 'Update Service Type', 'textdomain-cpts' ),
		'view_item'                  => __( 'View Service Type', 'textdomain-cpts' ),
		'separate_items_with_commas' => __( 'Separate Service Types with commas', 'textdomain-cpts' ),
		'add_or_remove_items'        => __( 'Add or remove Service Types', 'textdomain-cpts' ),
		'choose_from_most_used'      => __( 'Choose from the most used', 'textdomain-cpts' ),
		'popular_items'              => __( 'Popular Service Types', 'textdomain-cpts' ),
		'search_items'               => __( 'Search Service Types', 'textdomain-cpts' ),
		'not_found'                  => __( 'Not Found', 'textdomain-cpts' ),
		'no_terms'                   => __( 'No Service Types', 'textdomain-cpts' ),
		'items_list'                 => __( 'Service Types list', 'textdomain-cpts' ),
		'items_list_navigation'      => __( 'Service Types list navigation', 'textdomain-cpts' ),
	);

	$service_type_args   = array(
		'labels'            => $service_type_labels,
		'hierarchical'      => true,
		'public'            => true,
		'show_ui'           => true,
		'show_admin_column' => true,
		'description'       => __( 'The type of service.', 'textdomain-cpts' ),
		'show_in_nav_menus' => true,
		'show_tagcloud'     => false,
		'rewrite'           => array(
			'slug' => 'service_type',
		),
	);

	register_taxonomy( 'service_type', array( 'service' ), $service_type_args );

	register_taxonomy_for_object_type( 'service_type', 'service' ); 
	
}

add_action( 'init', register_custom_post_types, 0 );

以上是关于php 创建具有自定义分类的自定义帖子类型的主要内容,如果未能解决你的问题,请参考以下文章

未找到具有自定义分页 404 的自定义分类和自定义帖子类型

PHP 按照任何帖子类型的自定义分类的条款列出帖子

具有相同分类术语中的下一个和上一个帖子的自定义帖子类型

php 带有分类的自定义帖子类型中单个项目的面包屑

无法获取自定义分类存档页面以显示帖子

如何在自定义帖子类型永久链接中添加自定义分类?