WordPress自定义文章和自定义类型代码参考

自定义文章类型(custom post type):

// Register post type for instructors
function gymfitness_instructors() {

	$labels = array(
		'name'                  => _x( 'Instructors', 'Post Type General Name', 'gymfitness' ),
		'singular_name'         => _x( 'Instructor', 'Post Type Singular Name', 'gymfitness' ),
		'menu_name'             => __( 'Instructors', 'gymfitness' ),
		'name_admin_bar'        => __( 'Instructor', 'gymfitness' ),
		'archives'              => __( 'Archive', 'gymfitness' ),
		'attributes'            => __( 'Attributes', 'gymfitness' ),
		'parent_item_colon'     => __( 'Parent Instructor', 'gymfitness' ),
		'all_items'             => __( 'All Instructors', 'gymfitness' ),
		'add_new_item'          => __( 'Add Instructor', 'gymfitness' ),
		'add_new'               => __( 'Add Instructor', 'gymfitness' ),
		'new_item'              => __( 'New Instructor', 'gymfitness' ),
		'edit_item'             => __( 'Edit Instructor', 'gymfitness' ),
		'update_item'           => __( 'Update Instructor', 'gymfitness' ),
		'view_item'             => __( 'View Instructor', 'gymfitness' ),
		'view_items'            => __( 'View Instructors', 'gymfitness' ),
		'search_items'          => __( 'Search Instructor', 'gymfitness' ),
		'not_found'             => __( 'Not Found', 'gymfitness' ),
		'not_found_in_trash'    => __( 'Not Found in Trash', 'gymfitness' ),
		'featured_image'        => __( 'Featured Image', 'gymfitness' ),
		'set_featured_image'    => __( 'Save Featured Image', 'gymfitness' ),
		'remove_featured_image' => __( 'Remove Featured Image', 'gymfitness' ),
		'use_featured_image'    => __( 'Use as Featured Image', 'gymfitness' ),
		'insert_into_item'      => __( 'Insert in Instructor', 'gymfitness' ),
		'uploaded_to_this_item' => __( 'Add in Instructor', 'gymfitness' ),
		'items_list'            => __( 'List Instructors', 'gymfitness' ),
		'items_list_navigation' => __( 'Navigate to Instructors', 'gymfitness' ),
		'filter_items_list'     => __( 'Filter Instructors', 'gymfitness' ),
	);
	$args = array(
		'label'                 => __( 'Instructors', 'gymfitness' ),
		'description'           => __( 'Instructors for website', 'gymfitness' ),
		'labels'                => $labels,
		'supports'              => array( 'title', 'editor', 'thumbnail' ),
		'hierarchical'          => false,
		'public'                => true,
		'show_ui'               => true,
		'show_in_menu'          => true,
        'menu_position'         => 7,
        'menu_icon'             => 'dashicons-admin-users',
		'show_in_admin_bar'     => true,
		'show_in_nav_menus'     => true,
		'can_export'            => true,
		'has_archive'           => false,
		'exclude_from_search'   => false,
		'publicly_queryable'    => true,
		'capability_type'       => 'page',
	);
	register_post_type( 'instructors', $args );

}
add_action( 'init', 'gymfitness_instructors', 0 );

创建插件文件夹,创建custom_post_type.php文件,然后粘贴上面的代码进去,根据自己的需要修改。插件头部要设置安全验证: if ( !defined(ABSPATH)) {

die(); } 后台开启插件,就可以看到自定义的post type.

自定义分类(Taxonomy):

//add custom taxonomies

add_action( 'init', 'creat_weblinks_taxonomies',0 );

function creat_weblinks_taxonomies() {
  // Add new taxonomy, make it hierarchical (like categories)
  $labels = array(
    'name'              => _x( 'links category', 'taxonomy general name', 'textdomain' ),
    'singular_name'     => _x( 'link category', 'taxonomy singular name', 'textdomain' ),
    'search_items'      => __( 'Search links category', 'textdomain' ),
    'all_items'         => __( 'All links category', 'textdomain' ),
    'parent_item'       => __( 'Parent links category', 'textdomain' ),
    'parent_item_colon' => __( 'Parent links category:', 'textdomain' ),
    'edit_item'         => __( 'Edit links category', 'textdomain' ),
    'update_item'       => __( 'Update links category', 'textdomain' ),
    'add_new_item'      => __( 'Add New links category', 'textdomain' ),
    'new_item_name'     => __( 'New links category Name', 'textdomain' ),
    'menu_name'         => __( 'links category', 'textdomain' ),
  );

  $args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'show_in_rest'      => true,
    
  );


  register_taxonomy('resources_links',array('resource_links'),$args );
}

WordPress官方自定义分类模板:

function wporg_register_taxonomy_course()
{
    $labels = [
        'name'              => _x('Courses', 'taxonomy general name'),
'singular_name'     => _x('Course', 'taxonomy singular name'),
'search_items'      => __('Search Courses'),
'all_items'         => __('All Courses'),
'parent_item'       => __('Parent Course'),
'parent_item_colon' => __('Parent Course:'),
'edit_item'         => __('Edit Course'),
'update_item'       => __('Update Course'),
'add_new_item'      => __('Add New Course'),
'new_item_name'     => __('New Course Name'),
'menu_name'         => __('Course'),
];
$args = [
'hierarchical'      => true, // make it hierarchical (like categories)
'labels'            => $labels,
'show_ui'           => true,
'show_admin_column' => true,
'query_var'         => true,
'rewrite'           => ['slug' => 'course'],
];
register_taxonomy('course', ['post'], $args);
}
add_action('init', 'wporg_register_taxonomy_course');
Share :
Tag :