WordPress Custom Post Explained

By default wordpress have 5 major post type. They are as: Post, Page,  Attachement,  Revision and Nav Menus. In cases when the functionality provided by these default post aren’t enough, we create custom post types. Custom post types as the term goes are the customised post that are custom made based on your requirements. It can […]

By default wordpress have 5 major post type. They are as: Post, Page,  Attachement,  Revision and Nav Menus.

In cases when the functionality provided by these default post aren't enough, we create custom post types. Custom post types as the term goes are the customised post that are custom made based on your requirements. It can have added functionalities that the default posts wouldn't have.

So How do we create custom post?

A custom post is created by registering it as a post type. Wordpress function register_post_type is used to create custom post types. Various definitons are allowed to define inside the function as the singular/plural name, rewrite rules, display options such as dispaly it publicly or admin dashboard etc.

register_post_type Explained

As discussed above the function creates or modifies the post type. It takes two parameters viz post type and arguments.

The first parameter is the post type and second parameter is an array of arguments. The list of arguments that can go inside the array can be found here

A sample Custom post Code

[php]
add_action( 'init', 'create_car_reviews' );

function create_car_reviews() {
register_post_type( 'car_reviews',
array(
'labels' => array(
'name' => __( 'Car Reviews' ),
'singular_name' => __( 'Car Review' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'rewrite' => array('slug' => 'videos')
)
);
}
[/php]

So what we did above

We created a custom post called Car Review, refrenced via singular name. We initialised it via a wordpress hook by calling add_action method. We defined several custom structure for post as different labels, display option to display on dashboard and publicly by defining public property to true, make it support title, editor and thumbnail via defining supports and route is set via rewrite rule.

How to display the custom post now?
As the default post are displayed via single.php file. Custom post are displayed via single-[postype name].php, where postype name refers to the first attribute inserted in the register_post_type function above.

So our php file that will display an individual car review post will be single-car_reviews.php

Sample Code for single-car_reviews.php

[php]
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<div id="post-content">
<h1><?php the_title();?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>
[/php]

About Author

Robin Thebe

Digital Strategist based in Sydney.

I am multi disciplined web developer based in Sydney focusing around WordPress web design, wordpress development, SEO, SEM and Email Marketing.