WP Easy Tuts

WpEasyTuts » Tutorials

How to Create Custom Post Types In WordPress Without Plugin

Posted on October 11, 2017 by

Create Custom Post Types Without Plugin in WordPress

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:

http://www.yourdomain.com/games/premier-football-match-2017

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:

http://www.yourdomain.com/leagues/football

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.

http://yourdomain.com/games
http://yourdomain.com/leagues/football

The above URLs will use archive-leagues.php template to display your posts. The first url basically show all custom posts. Second URL will display custom posts for specific category.

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.

Final Words

  1. We created a function for the custom posts. We have added the function inside functions.php file located in our theme folder. For our case custom posts name is “Games”.
  2. We created a function which allow us to create categories for custom posts. For our case the taxonomy for custom post categories is “leagues”.
  3. We added the query code to show most recent custom posts.
  4. We duplicate the archive.php file and renamed it archive-leagues.php so we can modify the design to display all custom posts categories. We also did rename single.php to single-games.php to modify the design for all custom posts single page.

1 Comment

  • Reply wp freelancer May 30, 2018 at 10:35 am

    Excellent tutorial! It saved lot of time. It just made my work easier. Good jobs and awesome tutorial.

    Thanks.

  • Leave a Reply