How to create custom post types in WordPress

If you need different kind of post rather than regular post then you have to create custom post types. Creating custom post types is not so difficult, WordPress’s open source code has made it easy for developers.

You can also install a plugin to create custom post types from WordPress repository. You can download this Custom Post Type UI plugin from WordPress.org website.

Custom Post Types Plugin

 

Now install the plugin, if you do not know how to install a plugin, you can learn it here How to Install a WordPress plugin. and lets move forward, login to WordPress admin panel and you will see a new menu item named CPT UI. Click on CPT UI > Add/Edit Post Types and you will get the below screen.

 

Custom Post Types admin area
Custom Post Types admin area

 

Now you can input your new custom post type’s name, slug, description, label etc.

 

Custom Post Types Taxonomies
Custom Post Types Taxonomies

 

To add taxonomies, go to CPT UI > Add/Edit Taxonomies and you will get the above screen. After completing the settings you can easily post new type item from admin area.

 

WordPress Custom Post Types with Codes:

Using plugin is always a burden for a site, a website has to load it’s assets and causes delay in loading, another problem is if you delete the plugin, all custom post types will be deleted and the site show broken pages. Thats why if you have a minimum PHP knowledge and a understanding of WordPress theme’s hierarchy you can create custom post types easily.

In functions.php, copy/paste the following codes and check your admin panel that, you get the menu for custom post types

 

// Register Post Type: Portfolio
add_action( 'init', 'kt_register_posttype' );
function kt_register_posttype() {
$labels = array(
'name' => _x( 'Portfolios', 'Post Type General Name', 'themetext' ),
'singular_name' => _x( 'Portfolio', 'Post Type Singular Name', 'themetext' ),
'menu_name' => __( 'Portfolios', 'themetext' ),
'name_admin_bar' => __( 'Portfolio', 'themetext' ),
'archives' => __( 'Item Archives', 'themetext' ),
'attributes' => __( 'Item Attributes', 'themetext' ),
'parent_item_colon' => __( 'Parent Item:', 'themetext' ),
'all_items' => __( 'All Items', 'themetext' ),
'add_new_item' => __( 'Add New Item', 'themetext' ),
'add_new' => __( 'Add New', 'themetext' ),
'new_item' => __( 'New Portfolio', 'themetext' ),
'edit_item' => __( 'Edit Portfolio', 'themetext' ),
'update_item' => __( 'Update Portfolio', 'themetext' ),
'view_item' => __( 'View Portfolio', 'themetext' ),
'view_items' => __( 'View Portfolios', 'themetext' ),
'search_items' => __( 'Search Portfolio', 'themetext' ),
'not_found' => __( 'Not found', 'themetext' ),
'not_found_in_trash' => __( 'Not found in Trash', 'themetext' ),
'featured_image' => __( 'Featured Image', 'themetext' ),
'set_featured_image' => __( 'Set featured image', 'themetext' ),
'remove_featured_image' => __( 'Remove featured image', 'themetext' ),
'use_featured_image' => __( 'Use as featured image', 'themetext' ),
'insert_into_item' => __( 'Insert into item', 'themetext' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'themetext' ),
'items_list' => __( 'Items list', 'themetext' ),
'items_list_navigation' => __( 'Items list navigation', 'themetext' ),
'filter_items_list' => __( 'Filter items list', 'themetext' ),
);
$args = array(
'label' => __( 'Portfolio', 'themetext' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'post-formats' ),
'taxonomies' => array( 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-marker',
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'portfolio', $args );
}

 

I have registered here portfolio as a post type. You may need it in your portfolio theme. Upon placing the above codes in functions.php , you will see the Portfolio custom post type.

 

How to show custom post types in a page?

To show the custom post types on a page, you need to create a custom query. Look at the below codes, you will understand:

 

<?php

if ( get_query_var( 'paged' ) ) { 
$paged = get_query_var( 'paged' ); 
} elseif ( get_query_var( 'page' ) ) { 
$paged = get_query_var( 'page' ); 
} else { 
$paged = 1; 
}

$args = array(
'post_type'=>'airdrop', // Your post type name
'posts_per_page' =>5,
'paged' => $paged,
);

$loop = new WP_Query( $args );
global $paged;
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); ?>

// Title and contents will go here

<?php 
endwhile; 
wp_reset_postdata();
?>

 

keep the above codes in a custom page template or in index.php file, your custom post types will show on the site. If you paste the code in the index.php then it will be your default post page so its better to create a custom page template. If you do not know you can learn from here how to create a custom page template in WordPress . You can show your all custom post posts on front page with the above codes. Let me know if you have any query about this.

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top