How to Add Custom Post Type and Taxonomies to WordPress
How to create a custom post type that can be used as a portfolio. You can also add custom categories to your new post types.
Paste to your child theme's functions.php
file
/**
* Register custom post types
*
* @since 1.0
*/
function my_custom_post_types() {
register_post_type( 'portfolio', array(
'public' => true,
'menu_position' => 20,
'taxonomies' => array( 'portfolio_category' ),
'has_archive' => false,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'labels' => array(
'name' => 'Portfolio',
'singular_name' => 'Portfolio',
'menu_name' => 'Portfolio',
'name_admin_bar' => 'Portfolio',
'add_new_item' => 'Add New Portfolio Item',
'edit_item' => 'Edit Portfolio Item',
'new_item' => 'New Portfolio Item',
'view_item' => 'View Portfolio Item',
'search_items' => 'Search Portfolio',
'not_found' => 'Nothing found',
'not_found_in_trash' => 'Nothing found in trash',
'all_items' => 'View All'
)
) );
}
add_action( 'init', 'my_custom_post_types', 1 );
/**
* Create taxonomies to associate with post type.
*
* @since 1.0
*/
function my_custom_taxonomies() {
register_taxonomy( 'portfolio_category', 'portfolio', array(
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'portfolio/category',
'with_front' => false,
'hierarchical' => true
),
'capabilities' => array( 'manage_categories' )
) );
}
add_action( 'init', 'my_custom_taxonomies', 0 );
SEE WHAT'S NEW IN MD
The Next New Version of Marketers Delight is Almost Hereā¦
There's always something new in the works here in the MD Labs. Join the email list to get the newest updates, snippets, and videos sent straight to your e-mail inbox. *