One of the most powerful features of WordPress is the ability to create custom post types. By default, WordPress comes with several post types such as Posts, Pages, and Attachments. However, sometimes you need to create your own post types to better organize your content and make it easier for your visitors to find what they’re looking for. In this blog, we’ll show you how to create a custom post type in WordPress without using a plugin.
Step 1: Add Custom Post Type to Functions.php
The first step is to add the custom post type to your theme’s functions.php file. To do this, open your theme’s functions.php file in a text editor and add the following code:
[dm_code_snippet background=”no” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
function create_custom_post_type() { register_post_type( 'custom_post_type', array( 'labels' => array( 'name' => __( 'Custom Post Type' ), 'singular_name' => __( 'Custom Post Type' ) ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'custom-post-type'), ) ); } add_action( 'init', 'create_custom_post_type' );
[/dm_code_snippet]
This code will create a custom post type called “Custom Post Type” with a slug of “custom-post-type”.
Step 2: Customize Your Custom Post Type
Now that you’ve created your custom post type, you can customize it to fit your needs. You can add additional labels, change the slug, and add custom fields. Here’s an example of how to add a custom field:
[dm_code_snippet background=”no” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
function add_custom_field_to_post_type() { add_meta_box( 'custom_field', __( 'Custom Field' ), 'custom_field_callback', 'custom_post_type' ); } add_action( 'add_meta_boxes', 'add_custom_field_to_post_type' ); function custom_field_callback() { global $post; $value = get_post_meta( $post->ID, '_custom_field', true ); echo ''; } function save_custom_field_data( $post_id ) { if( isset( $_POST['custom_field'] ) ) { update_post_meta( $post_id, '_custom_field', sanitize_text_field( $_POST['custom_field'] ) ); } } add_action( 'save_post_custom_post_type', 'save_custom_field_data' );
[/dm_code_snippet]
This code will add a custom field called “Custom Field” to your custom post type. You can customize the field by changing the label and field type.
Step 3: Display Your Custom Post Type
Finally, you can display your custom post type on your website. You can do this by creating a custom template file for your custom post type or by using a shortcode. Here’s an example of how to display your custom post type using a shortcode:
[dm_code_snippet background=”no” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
function custom_post_type_shortcode( $atts ) { $args = array( 'post_type' => 'custom_post_type', 'posts_per_page' => 10 ); $query = new WP_Query( $args ); if( $query->have_posts() ) { $output = '
-
- ‘; while( $query->have_posts() ) { $query->the_post(); $output .= ‘
- ‘ . get_the_title() . ‘
‘; } $output .= ‘
‘; wp_reset_postdata(); } return $output; } add_shortcode( ‘custom_post_type’, ‘custom_post_type_shortcode
[/dm_code_snippet]