How to Create and Display Custom Post Type in WordPress

Add Tool

Create and Display Custom Post Type in WordPress

Custom post type is the best functionality WordPress has. Now question is, what is custom post type? WordPress has many default post types as we see on our admin side like Posts, Pages, Revisions, Media and Navigation Menus. We can build our own custom post types and query them on the front-end. In this article, I’ll show you how to create and display custom post types in WordPress.

Creating Custom Post Types

 We can create as many custom post types as we want like this:

Custom Post Type in WordPress

Now I’m creating a custom post type “Test”. My custom post type would be like this:

Now I can create my own post panel and add my posts. Now I’m giving you the full code snippet for adding this functionality to your WordPress site. Paste the following code in your functions.php.


add_action( 'init', 'register_cpt_test' );
function register_cpt_test() {
$labels = array(
'name' => _x( 'Test', 'test' ),
'singular_name' => _x( 'test', 'test' ),
'add_new' => _x( 'Add New', 'test' ),
'add_new_item' => _x( 'Add New test', 'test' ),
'edit_item' => _x( 'Edit test', 'test' ),
'new_item' => _x( 'New test', 'test' ),
'view_item' => _x( 'View test', 'test' ),
'search_items' => _x( 'Search Test', 'test' ),
'not_found' => _x( 'No test found', 'test' ),
'not_found_in_trash' => _x( 'No test found in Trash', 'test' ),
'parent_item_colon' => _x( 'Parent test:', 'test' ),
'menu_name' => _x( 'Test', 'test' ),
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'Hi, this is my custom post type.',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => '',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
register_post_type( 'test', $args );

How to display custom post types

Displaying custom post types in your pages is same we display our default posts on pages. But for custom post types, we have to defined post type by name as you will observe in query_posts() below. Open your index.php file or page template file where you want to show your custom post type and paste the following code:

<?php query_posts( array( 'post_type' => array('post', 'test') ) ); ?>
<?php while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>

Detail Page for Custom Post Types like single.php

This tutorial is not finished yet, because now we have to create a detail page for custom post types. In WordPress template hierarchy, single.php is the default file for displaying post detail page. But in case of custom post types, it won’t work. We have to create a separate detail page for custom post types. Syntax for creating custom post type single page is:
single-{custom post type name}.php
So, my custom post type’s name is ‘test‘, I will create single-test.php for the detail page. Now we can put the code in our new single file as we do with our default single.php. For querying loop, you won’t have to define query_posts( array( ‘post_type’ => array(‘post’, ‘test’) ) ); in your single page. Just loop like default single file. For Example:

<?php if(have_posts()): while(have_posts()): the_post(); ?>

<?php the_title(); ?>

<?php the_content(); ?>

<?php endwhile; endif; ?>

Tags: custom post type plugin, custom post type tutorial, custom post type template, custom post type taxonomy, custom post type category

About Masum Billah

Create and Display Custom Post Type in WordPress 1stWebSuite website seo tool is a free website ranking tool site provides a lot of on page and off page seo useful tools for you. On this site, we have a large number of website ranking tools


Discuss about: How to Create and Display Custom Post Type in WordPress