Custom Post type with Buildin WordPress Categories
-
Hello! I want to create a custom post type for my website without using any plugin the Custom post type for example is Portfolio (Cant use jetpack one) and i want to make it categories as my WordPress post categories.
2- My website Permalink is domain.com/blog/%category%/%postname%/%post_id%/
So i want my portfolio as domain.com/portfolio/
not like domain.com/blog/portfolio/ because when i use plugin is shows blog after domain. and i want my categories like domain.com/portfolio/category/ Can anyone help me im building a custom website i need help.
-
If you’re coming from a user background it would be better/easier to just user a plugin such as Custom Post Type UI. If you’re familiar with development, you can start looking into
register_post_type()
and, if you don’t want to use the built-in categories,register_taxonomy()
.In addition to what Howdy suggested, you will need to use the Rewrite API to get permastructs the way you want.
If you create a CPT ‘portfolio’, by default the URL example.com/portfolio/{$cat-term}/ will attempt to find a single portfolio post whose slug is {$cat-term}. Rewrite rules will allow such a request to be handled differently, but you will then need an alternative permastruct for single portfolio posts. You could use any other base term that’s not already in use in place of ‘portfolio’ for requesting single portfolio posts, perhaps example.com/recent-work/{$portfolio-slug}/ or what ever.
You will first need to build your own CPT and link it with a Custom Taxonomy:
This is a CPT(I use it in my very own personal website.) for Portfolios. You will need to change kevinurielfonseca for yours theme name.
<?php function custom_portfolio_type() { // Set UI labels for Custom Post Type $portfolio_labels = array( 'name' => _x( 'Portfolios', 'Post Type General Name', 'kevinurielfonseca' ), 'singular_name' => _x( 'Portfolio', 'Post Type Singular Name', 'kevinurielfonseca' ), 'menu_name' => __( 'Portfolios', 'kevinurielfonseca' ), 'name_admin_bar' => __( 'Portfolio', 'kevinurielfonseca' ), 'archives' => __( 'Portfolio Archives', 'kevinurielfonseca' ), 'attributes' => __( 'Portfolio Attributes', 'kevinurielfonseca' ), 'parent_item_colon' => __( 'Parent Portfolio', 'kevinurielfonseca' ), 'all_items' => __( 'All Portfolios', 'kevinurielfonseca' ), 'add_new_item' => __( 'Add New Portfolio', 'kevinurielfonseca' ), 'add_new' => __( 'Add Portfolio', 'kevinurielfonseca' ), 'new_item' => __( 'New Item', 'kevinurielfonseca' ), 'edit_item' => __( 'Edit Portfolio', 'kevinurielfonseca' ), 'update_item' => __( 'Update Portfolio', 'kevinurielfonseca' ), 'view_item' => __( 'View Portfolio', 'kevinurielfonseca' ), 'view_items' => __( 'View Portfolios', 'kevinurielfonseca' ), 'search_items' => __( 'Search Portfolio', 'kevinurielfonseca' ), 'not_found' => __( 'Not Found', 'kevinurielfonseca' ), 'not_found_in_trash' => __( 'Not found in Trash', 'kevinurielfonseca' ), 'featured_image' => __( 'Featured Image', 'kevinurielfonseca' ), 'set_featured_image' => __( 'Set featured image', 'kevinurielfonseca' ), 'remove_featured_image' => __( 'Remove featured image', 'kevinurielfonseca' ), 'use_featured_image' => __( 'Use as featured image', 'kevinurielfonseca' ), 'insert_into_item' => __( 'Insert into item', 'kevinurielfonseca' ), 'uploaded_to_this_item' => __( 'Uploaded to this item', 'kevinurielfonseca' ), 'items_list' => __( 'Items list', 'kevinurielfonseca' ), 'items_list_navigation' => __( 'Items list navigation', 'kevinurielfonseca' ), 'filter_items_list' => __( 'Filter items list', 'kevinurielfonseca' ), ); // Set other options for Custom Post Type $portfolio_args = array( 'label' => __( 'portfolios', 'kevinurielfonseca' ), 'description' => __( 'Portfolio Templates and Snippets', 'kevinurielfonseca' ), 'labels' => $portfolio_labels, // Features this CPT supports in Post Editor 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions','post-formats'), // You can associate this CPT with a taxonomy or custom taxonomy. 'taxonomies' => array( 'portfolio_categories'), /* A hierarchical CPT is like Pages and can have * Parent and child items. A non-hierarchical CPT * is like Posts. */ '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', 'menu_icon' => 'dashicons-format-gallery', ); // Registering your Custom Post Type register_post_type( 'portfolios', $portfolio_args ); } /* Hook into the 'init' action so that the function * Containing our post type registration is not * unnecessarily executed. */ add_action( 'init', 'custom_portfolio_type', 0 ); ?>
and here is the Custom Taxonomy for the CPT that I pasted above:
<?php //========================= Taxonomy for Portfolio Custom Post Type ===========================// //create a custom taxonomy name it topics for your posts function portfolio_categories_taxonomy() { // Add new taxonomy, make it hierarchical like categories //first do the translations part for GUI $portfolio_cats = array( 'name' => _x( 'Portfolio Categories', 'taxonomy general name' ), 'singular_name' => _x( 'Portfolio Category', 'taxonomy singular name' ), 'search_items' => __( 'Search Portfolio Categories' ), 'all_items' => __( 'All Portfolio Categories' ), 'parent_item' => __( 'Parent Portfolio Category' ), 'parent_item_colon' => __( 'Parent Portfolio Category:' ), 'edit_item' => __( 'Edit Portfolio Category' ), 'update_item' => __( 'Update Portfolio Category' ), 'add_new_item' => __( 'Add New Portfolio Category' ), 'new_item_name' => __( 'New Portfolio Category' ), 'menu_name' => __( 'Portfolio Categories' ), ); // Now register the taxonomy register_taxonomy('portfolio_categories',array('portfolios'), array( 'hierarchical' => true, 'labels' => $portfolio_cats, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'portfolio_categories' ), )); } add_action( 'init', 'portfolio_categories_taxonomy', 0 ); //========================= Tags for Portfolio Custom Type ===========================// //hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires function portfolio_tags_taxonomy() { // Labels part for the GUI $portfolio_tags = array( 'name' => _x( 'Portfolio Tags', 'taxonomy general name' ), 'singular_name' => _x( 'Portfolio Tag', 'taxonomy singular name' ), 'search_items' => __( 'Search Portfolio Tags' ), 'popular_items' => __( 'Popular Portfolio Tags' ), 'all_items' => __( 'All Portfolio Tags' ), 'parent_item' => null, 'parent_item_colon' => null, 'edit_item' => __( 'Edit Portfolio Tag' ), 'update_item' => __( 'Update Portfolio Tag' ), 'add_new_item' => __( 'Add New Portfolio Tag' ), 'new_item_name' => __( 'New Portfolio Tag Name' ), 'separate_items_with_commas' => __( 'Separate portfolio tags with commas' ), 'add_or_remove_items' => __( 'Add or remove portfolio tags' ), 'choose_from_most_used' => __( 'Choose from the most used portfolio tags' ), 'menu_name' => __( 'Portfolio Tags' ), ); // Now register the non-hierarchical taxonomy like tag register_taxonomy('portfolio_tags',array('portfolios'),array( 'hierarchical' => false, 'labels' => $portfolio_tags, 'show_ui' => true, 'show_admin_column' => true, 'update_count_callback' => '_update_post_term_count', 'query_var' => true, 'rewrite' => array( 'slug' => 'portfolio_tags' ), )); } add_action( 'init', 'portfolio_tags_taxonomy', 0 ); ?>
I pretty much give you everything you need. Now the only thing you have left to by your own is the custom page template in order to display the posts under the CPT(Portfolio). I can actually give you a link to my website in which I explain how to do it (detail by detail) but its now allowed to advertise URL’s
- The topic ‘Custom Post type with Buildin WordPress Categories’ is closed to new replies.