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 a custom post type.
Suppose you have a hotel website that lists difference destinations around the world and you want to include a directory of hotels – there are many ‘business directory plugins’ – but a lot of those are premium plugins that cost an annual licence – By default you already have pages and posts for blog. You can add a simple function to create a separate post type for hotels, in this way, you can manage your hotel listings separately.
Lets create custom post type in WordPress
Step 1: In your favourite text editor, open up the functions.php file inside your theme folder.
At the bottom of the file – add the following code.
function affordweb_hotels_post() { $labels = array( 'name' => _x( 'Hotsl', 'post type general name' ), 'singular_name' => _x( 'Hotel', 'post type singular name' ), 'add_new' => _x( 'Add New', 'game' ), 'add_new_item' => __( 'Add New Hotel' ), 'edit_item' => __( 'Edit Hotel' ), 'new_item' => __( 'New Hotel' ), 'all_items' => __( 'All Hotels' ), 'view_item' => __( 'View Hotel' ), 'search_items' => __( 'Search Hotels' ), 'not_found' => __( 'No hotels found' ), 'not_found_in_trash' => __( 'No hotels found in the Trash' ), 'parent_item_colon' => '', 'menu_name' => 'Hotels' ); $args = array( 'labels' => $labels, 'description' => 'Holds hotel specific data', 'public' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), 'has_archive' => true, ); register_post_type( 'hotel', $args ); } add_action( 'init', 'custom_post_event' );
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/hotels/the-queens-hotel-blackpool
Step 2: Now we have already created the custom post in WordPress, we need 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 affordweb_taxonomies_hotels() { $labels = array( 'name' => _x( 'Hotel Categories', 'taxonomy general name' ), 'singular_name' => _x( 'Hotel Category', 'taxonomy singular name' ), 'search_items' => __( 'Search Hotel Categories' ), 'all_items' => __( 'All Hotels Categories' ), 'parent_item' => __( 'Parent Hotel Category' ), 'parent_item_colon' => __( 'Parent Hotel Category:' ), 'edit_item' => __( 'Edit Hotel Category' ), 'update_item' => __( 'Update Hotel Category' ), 'add_new_item' => __( 'Add New Hotel Category' ), 'new_item_name' => __( 'New Hotel Category' ), 'menu_name' => __( 'Hotel Categories' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, ); register_taxonomy( 'location', 'hotels', $args ); } add_action( 'init', 'affordweb_taxonomies_hotels', 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/location/blackpool
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-hotels.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
- 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”.
- We created a function which allow us to create categories for custom posts. For our case the taxonomy for custom post categories is “leagues”.
- We added the query code to show most recent custom posts.
- 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.