Custom Post register, Custom Taxonomy register
How to Register a Custom Post and Custom Taxonomies
Welcome to all on my today tune. Today, I am going describe an important topic “How to register a custom post and custom taxonomies”. On this tune I am going to describe -how register custom post in WordPress, how register custom taxonomy in WordPress and how work both of them.
Custom Post Registration
Custom post very important to different post in WordPress. To get a custom post in WordPress dashboard must register custom post in theme functions.php file. To register a custom post take below code paste in you theme functions.php file.
add_action( 'init', 'register_custom_post_name' ); function register_custom_post_name() { register_post_type( 'portfolio', //Post type is unique name. This important to use this custom post. array( 'labels' => array( 'name' => __( 'Portfolios' ), 'singular_name' => __( 'Portfolio' ) ), 'public' => true, 'supports' => array('title', 'editor', 'custom-fields', 'thumbnail'), 'has_archive' => true, 'rewrite' => array('slug' => 'portfolio-item'), ) ); }
This is very simple code to register a custom post, If you take it you functions.php and save you can see a Portfolio custom post in your dashboard. Here register_post_type is unique name for this custom post. Name is the name of custom post here is portfolio, singular_name is singular name of custom post. Public is true means this custom-post post show everyone, supports is- what need to support this custom, has_archive is true means this custom-post post to have as archive. Rewrite slug is- what will common slug for this custom post.
Register Custom Taxonomy
WordPress has default taxonomy as category & tag. Sometimes may need custom taxonomy for theme working design or custom post. To get custom taxonomies in custom post need to register custom taxonomy in theme functions.php file. To register custom taxonomy taking below code in theme functions.php file.
function custom_post_taxonomy() { register_taxonomy( 'portfolio_category', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 'portfolio', //post type name array( 'hierarchical' => true, 'label' => 'Portfolio Category', //Display name 'query_var' => true, 'show_admin_column' => true, 'rewrite' => array( 'slug' => 'portfolio-category', // This controls the base slug that will display before each term 'with_front' => true // Don't display the category base before ) ) ); } add_action( 'init', 'custom_post_taxonomy');
This code describe in it by using WordPress comment system. If you can not understand comment on this post.
Finally both custom post can use together. The code are shown below. To work both custom post and custom taxonomy must be same is post_type which here use “portfolio”
add_action( 'init', 'register_custom_post_name' ); function register_custom_post_name() { register_post_type( 'portfolio', //Post type is unique name. This important to use this custom post. array( 'labels' => array( 'name' => __( 'Portfolios' ), 'singular_name' => __( 'Portfolio' ) ), 'public' => true, 'supports' => array('title', 'editor', 'custom-fields', 'thumbnail'), 'has_archive' => true, 'rewrite' => array('slug' => 'portfolio-item'), ) ); } function custom_post_taxonomy() { register_taxonomy( 'portfolio_category', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 'portfolio', //post type name array( 'hierarchical' => true, 'label' => 'Portfolio Category', //Display name 'query_var' => true, 'show_admin_column' => true, 'rewrite' => array( 'slug' => 'portfolio-category', // This controls the base slug that will display before each term 'with_front' => true // Don't display the category base before ) ) ); } add_action( 'init', 'custom_post_taxonomy');
Comments are closed