In this article we will teach you how to create custom post type in wordpress without plugin.
Basically WordPress has come with some default post types:
Sometimes we need to create our custom post types like news, movies, songs, albums, portfolios, projects, etc. And We can create this custom post type by two methods.
In this method you can create your custom post type by using the wordpress plugin Custom Post Type UI. Custom Post Type UI provides an easy to use interface for registering and managing custom post types and taxonomies for your website or blog.
If you want to create custom post type without plugin. Then paste the code given below in your functions.php file located at website_root_path/wp-content/themes/your_theme/functions.php. Here below we are going to create our custom post type songs.
/*
* Creating a function to create our Custom Post Type
*/
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Songs', 'Post Type General Name', 'twentytwenty' ),
'singular_name' => _x( 'Song', 'Post Type Singular Name', 'twentytwenty' ),
'menu_name' => __( 'Songs', 'twentytwenty' ),
'parent_item_colon' => __( 'Parent Song', 'twentytwenty' ),
'all_items' => __( 'All Songs', 'twentytwenty' ),
'view_item' => __( 'View Song', 'twentytwenty' ),
'add_new_item' => __( 'Add New Song', 'twentytwenty' ),
'add_new' => __( 'Add New', 'twentytwenty' ),
'edit_item' => __( 'Edit Song', 'twentytwenty' ),
'update_item' => __( 'Update Song', 'twentytwenty' ),
'search_items' => __( 'Search Song', 'twentytwenty' ),
'not_found' => __( 'Not Found', 'twentytwenty' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwenty' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'Songs', 'twentytwenty' ),
'description' => __( 'Top Songs Lists', 'twentytwenty' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'post-formats'),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'song_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',
'show_in_rest' => true,
);
// Registering your Custom Post Type
register_post_type( 'songs', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'custom_post_type', 0 );
In the code above we have used the function register_post_type to register our custom post type. And twentytwenty is our theme name you can replace it with your theme name. We have used here our custom taxonomies song_categories you can read how to create custom taxonomies in wordpress without plugin. You can create own taxonomies in wordpress. And you can read in detail about register_post_type in wordpress.
Now we need to display our custom post types in wordpress. By default Custom Post Types uses the Archives template. So, we can use the link http://example.com/songs url to displays our custom posts and replace example.com with your domain.
If you want to change layout of your custom posts type template then create a file archive-songs.php and paste the contents of the file archive.php and make your required changes. You can also take help from our article how to create custom post template in wordpress.
For single details page of custom post type wordpress uses the single.php’s template and if you want to modify the template then create a file single-songs.php file and paste the contents of single.php file and make your required changes. For this same you can take help from our article how to create custom post template in wordpress.
You can also use the code given below to loop through your custom post types in your templates.
<?php
$args = array( 'post_type' => 'songs', 'posts_per_page' => 10 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no songs matched your criteria.' ); ?></p>
<?php endif; ?>
In the code above we have used WP_Query to run query and used a post_type songs.
You can use the code given below to display your custom post types to your regular posts. Just paste the code given below to your functions.php file.
add_action( 'pre_get_posts', 'add_custom_post_types_to_query' );
function add_custom_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'songs' ) );
return $query;
}
In the code above we have use $query->set method and passed the array with values post and songs.
If you want to display custom post types in Widgets then you can use the plugin Ultimate Posts Widget. Just install and activate the plugin and drag into your sidebars.
If you want more tutorials and tricks about wordpress then visit our WordPress Page and follow us on facebook, twitter, tumblr, linkdedin and if you like this article then share this.