In this post, I am going to explain how to create custom post type with category/taxonomy in WordPress without any plugin.
There are lots of plugin available for custom post type and taxonomy but if you want to create without any plugin,then needs to follow given steps.
You can create custom post in two ways first use below code in your active theme functions.php file or create new own plugin and paste below code in your plugin file.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
add_action( 'init', 'webprepration_init' ); /** * Register a book post type. */ function webprepration_init() { $labels = array( 'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ), 'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ), 'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ), 'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ), 'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ), 'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ), 'new_item' => __( 'New Book', 'your-plugin-textdomain' ), 'edit_item' => __( 'Edit Book', 'your-plugin-textdomain' ), 'view_item' => __( 'View Book', 'your-plugin-textdomain' ), 'all_items' => __( 'All Books', 'your-plugin-textdomain' ), 'search_items' => __( 'Search Books', 'your-plugin-textdomain' ), 'parent_item_colon' => __( 'Parent Books:', 'your-plugin-textdomain' ), 'not_found' => __( 'No books found.', 'your-plugin-textdomain' ), 'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' ) ); $args = array( 'labels' => $labels, 'description' => __( 'Description.', 'your-plugin-textdomain' ), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'book' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ); register_post_type( 'book', $args ); } |
Now save file and check in admin new post type book is registered. you can create lots of custom post according to need using register_post_type(‘post_type_name’,$args); function.
This function takes two parameters first post type name and second array type.
Now for category/taxonomy WordPress provided predefined function register_taxonomy( ‘book-category’, array( ‘book’ ), $args );
This function adds or overwrites a taxonomy. It takes in a name, an object name that it affects, and an array of parameters. It does not return anything.
Example:- Please copy the below code and paste it in your active theme functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
add_action( 'init', 'create_book_taxonomies', 0 ); function create_book_taxonomies() { // Add new taxonomy, make it hierarchical (like categories) $labels = array( 'name' => _x( 'Categories', 'taxonomy general name', 'textdomain' ), 'singular_name' => _x( 'Book Category', 'taxonomy singular name', 'textdomain' ), 'search_items' => __( 'Search Categories', 'textdomain' ), 'all_items' => __( 'All Categories', 'textdomain' ), 'parent_item' => __( 'Parent Category', 'textdomain' ), 'parent_item_colon' => __( 'Parent Category:', 'textdomain' ), 'edit_item' => __( 'Edit Category', 'textdomain' ), 'update_item' => __( 'Update Category', 'textdomain' ), 'add_new_item' => __( 'Add New Category', 'textdomain' ), 'new_item_name' => __( 'New Category Name', 'textdomain' ), 'menu_name' => __( 'Book Category', 'textdomain' ), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'book-category' ), ); register_taxonomy( 'book-category', array( 'book' ), $args ); } |
If you want create multiple categories for single post then you can create lots of category there are no restriction in WordPress you can create lots of category like author,publication etc.
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.
Hello, the whole thing is going nicely here and ofcourse every one is sharing
facts, that’s truly good, keep up writing.
Thanks
Thanks!!!
I simply needed to thank you very much yet again. I do not know the things that I might have used in the absence of the entire creative concepts discussed by you directly on that field. Certainly was a real frustrating concern in my circumstances, however , seeing the specialised strategy you handled that made me to leap with fulfillment. I will be happy for your assistance and thus have high hopes you comprehend what a powerful job you have been carrying out educating most people through the use of your websites. I am certain you haven’t encountered all of us.
Your home is valueble for me. Thanks!…
I think this is one of the most vital info for me. And i am satisfied reading your article. But should commentary on few general issues, The website taste is wonderful, the articles is truly excellent : D. Excellent job, cheers
Thanks
Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!
Thanks for feedback
This is a awesome post and really helpful for me.
This is a awesome post and really helpful for me.
I simply want to mention I am just all new to blogs and definitely loved this web blog. More than likely I’m want to bookmark your website . You certainly have outstanding stories. Cheers for revealing your blog site.
These files are wonderful circumstances to customize the way the sport looks and they’re going to work well with Minecraft mods.
All your belongings will be expecting you for any limijted
timeframe, but they wait in places you died. Yes
it’s a game buut after immersing yourself you’re feeling truly happy that youu might have madde who you are a
great homje inside Minecraft.