WordPress

How to Create Custom Post Types In WordPress Without Plugin

Written by AskYouNow

WordPress is amazing content management system which provides you more than just a blogging platform. People can create any kind of website with any feature required with WordPress. One of the most powerful feature of WordPress is custom post type. By default user can create posts and pages in WordPress. However if you would like to have more than posts and pages in your website then you can simply add post type.

Suppose you have sports website where you would like to display different games schedule, posts for blog and pages such as contact, about us etc. By default you already have pages and posts for blog. You can add a simple function to create a separate post type for games. In this way, you can manage and design games section separately.

Lets create custom post type in WordPress

Step 1: Add custom post type function in functions.php file inside your theme folder.

				
					function easytuts_games_post() {
  $labels = array(
    'name'               => _x( 'Games', 'post type general name' ),
    'singular_name'      => _x( 'Game', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'game' ),
    'add_new_item'       => __( 'Add New Games' ),
    'edit_item'          => __( 'Edit Game' ),
    'new_item'           => __( 'New Game' ),
    'all_items'          => __( 'All Games' ),
    'view_item'          => __( 'View Game' ),
    'search_items'       => __( 'Search Games' ),
    'not_found'          => __( 'No game found' ),
    'not_found_in_trash' => __( 'No game found in the Trash' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Games'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds games and game specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'games', $args );
}
add_action( 'init', 'easytuts_games_post' );
				
			

The first part of above function define the labels for custom post type. You can change the label according to your post type. These label will appear on the back-end of WordPress same as for pages and post.

Now go to WordPress Admin and try to create a custom post type. The url structure for custom post should be as below:

Step 2: Since we already created custom post in WordPress we would like to create custom post categories as well. Similar post have categories we would also like to have categories for our custom post. Add the following function to create the custom post categories.

				
					function easytuts_taxonomies_games() {
  $labels = array(
    'name'              => _x( 'Game Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Game Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Game Categories' ),
    'all_items'         => __( 'All Games Categories' ),
    'parent_item'       => __( 'Parent Game Category' ),
    'parent_item_colon' => __( 'Parent Game Category:' ),
    'edit_item'         => __( 'Edit Game Category' ),
    'update_item'       => __( 'Update Game Category' ),
    'add_new_item'      => __( 'Add New Game Category' ),
    'new_item_name'     => __( 'New Game Category' ),
    'menu_name'         => __( 'Game Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
  );
  register_taxonomy( 'leagues', 'games', $args );
}
add_action( 'init', 'easytuts_taxonomies_games', 0 );
				
			

The above function will generate a category with permalink of “leagues”. You may change your permalink and modify labels according to your needs. The url structure for the category will be as follow:

Display Most Recent Custom Posts

Since we already created custom post as well categories for them. Now we need to show custom posts. We can show custom posts anywhere on the website. For example you may show custom posts on the Home page or sidebar.

				
					<h1>Recent Games Show</h1>
<?php
$post_args = array( 'post_type' => 'games', 'posts_per_page' => 8 );
$post_query = new WP_Query( $post_args );
 
if ( $post_query->have_posts() ) :
while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
 
<h2><?php the_title(); ?></h2>
<div class="posts-entry">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
 
<?php endwhile; // ending while loop ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no game matched your criteria.' ); ?></p>
<?php endif; // ending condition ?>
				
			

If you are familiar with WordPress Query then above code is pretty easy to understand. Basically in above code we have query the custom posts. You may add above code anywhere in your index.php, footer.php, sidebar.php or page.php to display most recent custom posts.

Custom Archive Page for Custom Posts

By default custom posts use the same archive page as your simple posts. But if you would like to modify archive page for the custom posts to show them in different way then it is pretty simple to do. Follow the steps below.

Step 1: Go to your theme folder and duplicate archive.php file to archive-leagues.php, We just need to add the custom posts taxonomy name along archive. Remember above we created “leagues” as taxonomy for the custom posts categories for games. You may replace “leagues” with your own custom taxonomy.

Step 2: Now all your custom posts with taxonomy “leagues” will use archive-leagues.php template. Any change in code made inside archives-leagues.php will effect only for custom posts archive page.

Custom Single Page for Custom Posts

Similar to archive page you can also create custom single page for the custom posts. It is pretty simple, just follow the steps below.

Step 1: Go to your theme folder and duplicate single.php file to single-games.php, Here we just add the custom post name along the single.php file. Above we created custom posts with name “games” so we renamed it to single-games.php. You may replace the “games” with your custom posts name.

Step 2: Now all the custom posts will use single-games.php template. You may edit the template and modify it according to your design.

We hope this article helped you learn how to change the sender’s name and email address in outgoing WordPress emails.

So that’s it for now, Hope you guys like this. We will come up with another blog post soon. Contact us for any query. Follow us on Facebook

About the author

AskYouNow

Leave a Comment

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code