How to create custom post type with category and extra fields?
Here we will explain to you how to create a custom post type with a category and extra fields in WordPress. This tutorial will help for making the custom post type so you can generate your posts with categories and show them on the front page.
Step 1)
Below is the code where you can create a custom post type with a category.
<?php
add_action( 'init', 'create_slider' );
function create_slider() {
register_post_type( 'slider',
array(
'labels' => array(
'name' => 'Slider',
'singular_name' => 'Slide',
'add_new' => 'Add New',
'add_new_item' => 'Add New Slide',
'edit' => 'Edit',
'edit_item' => 'Edit Slide',
'new_item' => 'New Slide',
'view' => 'View',
'view_item' => 'View Slide',
'search_items' => 'Search Slider',
'not_found' => 'No Slider found',
'not_found_in_trash' => 'No Slider found in Trash',
'parent' => 'Parent Slide'
),
'public' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'thumbnail' ),
//'taxonomies' => array( '' ),
//'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
'has_archive' => true
)
);
}
function slider_taxonomy() {
register_taxonomy(
'slider_category', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'slider', //post type name
array(
'hierarchical' => true,
'label' => 'Slider category', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'slider_cat', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'slider_taxonomy');
function add_post_format_filter_to_slider_dropdown($query){
if (function_exists('custom_post_type_filter_dropdown')) {
$cur_post_type = 'slider';
custom_post_type_filter_dropdown($cur_post_type,'slider_category',$query);
}
}
add_action('restrict_manage_posts','add_post_format_filter_to_slider_dropdown');
function add_post_format_filter_to_slider($query){
if (function_exists('custom_post_type_filter')) {
$cur_post_type = 'slider';
custom_post_type_filter($cur_post_type,'slider_category',$query);
}
}
add_action('pre_get_posts','add_post_format_filter_to_slider');
function slider_type_columns( $taxonomies ) {
$taxonomies[] = 'slider_category';
return $taxonomies;
}
add_filter( 'manage_taxonomies_for_slider_columns', 'slider_type_columns' );
add_action('init','add_custom_meta_boxes');
function add_custom_meta_boxes(){
add_action( 'add_meta_boxes', 'slide_heading_meta_box_add' );
add_action( 'save_post', 'slide_heading_meta_box_save' );
}
function slide_heading_meta_box_add()
{
add_meta_box( 'slide_heading', 'Custom URL/Heading', 'slide_heading_meta_box', 'slider', 'normal', 'high' );
}
function slide_heading_meta_box()
{
$custom_metas = get_post_custom( $post->ID );
$slide_url = isset( $custom_metas['slide_url'] ) ? esc_attr( $custom_metas['slide_url'][0] ) : '';
$slide_heading = isset( $custom_metas['slide_heading'] ) ? esc_attr( $custom_metas['slide_heading'][0] ) : '';
$heading_color = isset( $custom_metas['heading_color'] ) ? esc_attr( $custom_metas['heading_color'][0] ) : ''; ?>
<p>
<label for="fom_link3">URL: </label>
<input type="text" name="slide_url" id="slide_url" value="<?php echo $slide_url; ?>" size="34" />
<label for="fom_link3">Heading: </label>
<input type="text" name="slide_heading" id="slide_heading" value="<?php echo $slide_heading; ?>" size="34" maxlength="50" />
<label for="fom_link3">Color: </label>
<input type="text" name="heading_color" id="heading_color" value="<?php echo $heading_color; ?>" size="5" maxlength="10" />
</p><?php
}
function slide_heading_meta_box_save($post_id){
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array();
// Make sure your data is set before trying to save it
if( isset( $_POST['slide_url'] ) ) {
update_post_meta( $post_id, 'slide_url', $_POST['slide_url'] );
}
if( isset( $_POST['slide_heading'] ) ) {
update_post_meta( $post_id, 'slide_heading', $_POST['slide_heading'] );
}
if( isset( $_POST['heading_color'] ) ) {
update_post_meta( $post_id, 'heading_color', wp_kses( $_POST['heading_color'], $allowed ) );
}
}
Step 2)
Now visit the admin side, and you can see your custom post type with the category and extra fields there.
This is the very nice tutorial for beginner.
Very nice tutorial
What’s up, I check your blogs regularly. Your writing style is witty, keep doing what you’re doing!
Greetings! I’ve been following your site for some time now and finally got the courage to go ahead and give you a shout out from Lubbock Texas! Just wanted to say keep up the good work!
Ahaa, its fastidious discussion on the topic of this post at this place at this blog, I have read all that, so at this time me also commenting at this place.
You’ve made some good points there. I checked on the net to learn more about the issue and found most individuals will go along with your views on this website.
Hello! Do you know if they make any plugins to protect against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?
I will immediately grasp your rss feed as I can’t find your e-mail subscription link or newsletter service. Do you have any? Please let me recognise in order that I could subscribe. Thanks.
I am sure this article has touched all the internet viewers, its really really good post on building up new weblog.
Great post, you have pointed out some wonderful details, I likewise conceive this is a very great website.
I enjoy your writing style truly loving this web site.
I will immediately snatch your rss as I can not to find your email subscription link or e-newsletter service. Do you have any? Please permit me know in order that I could subscribe. Thanks.
Good response in return of this query with real arguments and describing all on the topic of that.
**mitolyn**
Mitolyn is a carefully developed, plant-based formula created to help support metabolic efficiency and encourage healthy, lasting weight management.
Hi to every single one, it’s really a nice for me to pay a quick visit this site, it consists of valuable Information.
Way cool! Some very valid points! I appreciate you writing this write-up plus the rest of the site is very good.
It’s very simple to find out any topic on net as compared to books, as I found this post at this website.
Also the generator works very quick and is able to add the roblox and tix to your account in a minute and really easy to make use of even a baby can use it so as to add robux to the account.
I am sure this article has touched all the internet viewers, its really really nice post on building up new webpage.
Ahaa, its pleasant conversation regarding this article at this place at this blog, I have read all that, so now me also commenting here.
I genuinely enjoy reading through on this web site, it has got wonderful content.
I like what you guys tend to be up too. Such clever work and reporting! Keep up the fantastic works guys I’ve incorporated you guys to our blogroll.
Thank you for sharing with us, I conceive this website really stands out :D.
I like what you guys tend to be up too. This type of clever work and exposure! Keep up the fantastic works guys I’ve added you guys to our blogroll.
Wow, this post is pleasant, my younger sister is analyzing such things, therefore I am going to tell her.
These are truly fantastic ideas in regarding blogging. You have touched some good things here. Any way keep up wrinting.
I love what you guys are up too. This kind of clever work and exposure! Keep up the great works guys I’ve incorporated you guys to my blogroll.
You made some really good points there. I looked on the net for more info about the issue and found most people will go along with your views on this site.
Hi, I do believe this is a great web site. I stumbledupon it 😉 I’m going to revisit once again since i have bookmarked it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.
Great article! That is the kind of information that are supposed to be shared across the web. Disgrace on Google for no longer positioning this post higher! Come on over and discuss with my web site . Thank you =)
I’ll immediately grab your rss feed as I can not find your email subscription hyperlink or newsletter service. Do you have any? Kindly allow me understand so that I could subscribe. Thanks.
These have some sort of special effect associated with them, similar to putting the enemy Pokemon to sleep or poisoning them.
Really no matter if someone doesn’t be aware of then its up to other people that they will help, so here it takes place.
Ahaa, its fastidious discussion on the topic of this post at this place at this webpage, I have read all that, so at this time me also commenting at this place.
Everyone loves what you guys are up too. This type of clever work and coverage! Keep up the fantastic works guys I’ve incorporated you guys to my blogroll.
Thank you for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
I am sure this piece of writing has touched all the internet viewers, its really really nice post on building up new weblog.
You’ve made some really good points there. I looked on the web for more information about the issue and found most individuals will go along with your views on this web site.