Example 1: get name custom post type wordpress
$pt = get_post_type_object( 'books' );
echo $pt->label;
echo $pt->labels->name;
echo $pt->labels->singular_name;
Example 2: custom post type
function custom_post_type() {
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'twentytwenty' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentytwenty' ),
'menu_name' => __( 'Movies', 'twentytwenty' ),
'parent_item_colon' => __( 'Parent Movie', 'twentytwenty' ),
'all_items' => __( 'All Movies', 'twentytwenty' ),
'view_item' => __( 'View Movie', 'twentytwenty' ),
'add_new_item' => __( 'Add New Movie', 'twentytwenty' ),
'add_new' => __( 'Add New', 'twentytwenty' ),
'edit_item' => __( 'Edit Movie', 'twentytwenty' ),
'update_item' => __( 'Update Movie', 'twentytwenty' ),
'search_items' => __( 'Search Movie', 'twentytwenty' ),
'not_found' => __( 'Not Found', 'twentytwenty' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwenty' ),
);
$args = array(
'label' => __( 'movies', 'twentytwenty' ),
'description' => __( 'Movie news and reviews', 'twentytwenty' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'genres' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type( 'movies', $args );
}
add_action( 'init', 'custom_post_type', 0 );
Example 3: wordpress create new post type
register_post_type( 'video',
array(
'labels' => array(
'name' => 'Videos',
'singular_name' => 'Video'
),
'public' => true,
'has_archive' => true,
'rewrite' => array(
'slug' => 'videos'
),
'exclude_from_search'=> true
,
'supports' => array(
'title','editor','thumbnail'
)
)
);
Example 4: create custom post type with category in wordpress functions.php
function create_posttype() {
register_post_type( 'wpll_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'products'),
)
);
}
add_action( 'init', 'create_posttype' );
Example 5: wordpress register post type
function book_setup_post_type() {
$args = array(
'public' => true,
'label' => __( 'Books', 'textdomain' ),
'menu_icon' => 'dashicons-book',
);
register_post_type( 'book', $args );
}
add_action( 'init', 'book_setup_post_type' );
Example 6: wp+create custom post type and category
add_action( 'init', 'create_news' );
function create_news ()
{
register_post_type( 'ecommerces',
array(
'labels' => array(
'name' => __( 'Ecommerces' ),
'singular_name' => __( 'Ecommerce' )
),
'public' => true,
'supports' => array ('title', 'editor', 'thumbnail')
)
);
register_taxonomy(
'ecommerce_cat',
'ecommerces',
array(
'labels' => array(
'name' => 'Ecommerce Categories',
'add_new_item' => 'Add New Ecommerce Category',
'new_item_name' => "New Ecommerce Category"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
'hasArchive' => true
)
);
}
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 100, 100, true );
}
?>