Amruta Nejkar

Post Types in WordPress

What Is a WordPress Custom Post Type?

Custom post types are used to convert a regular WordPress website into a content management system. As the name suggests, you can use custom post types to create various content types for your website.

By default in WordPress installation, including:

  • Post–blog post
  • Page – static page
  • Attachment – attached media
  • Revision – post revision
  • Navigation Menu – nav menu

When it comes to custom post types, WordPress supports an unlimited number of Custom Post Types. You can create your custom posts and call them up wherever you want.

For example, if you run a News website and want to add a custom post type titled “News.” Once created, the news post-type would have its own menu in the WordPress dashboard admin area. Additionally, you can create multiple post types such as Movies, Portfolios, etc.

How to Create a WordPress Custom Post Type

Follow the steps below to create a custom post type on a WordPress website:

  • Navigate to the function.php file from your WordPress theme directory
  • Add the following code to the function.php file

Once you’ve added the code, the News post type will automatically appear in the admin area 

When creating custom post types, it is necessary to use init for the hook in add_action(), and the register_post_type() function will take the arguments

$supports: Specifies that the post type is compatible and supports all essential features.

$labels: Specifies that the post type is referred correctly to the admin area.

$args: Specifies a permalink slug of the news and a menu position located just beneath the Posts menu.

Now let’s take a look at before and after adding custom post features to our WordPress website.

– Before adding features

The steps above tell how to register WordPress custom post types to the backend of any theme.

How to Create a New Custom Post on a WordPress Website

You can create a new custom post on your WordPress website by following the steps below:

  • Click the registered custom post type, which in our case is “News.”
  • Click Add New.
  • Type the title and body of your post.
  • Type the excerpt, and set a featured image.
  • Click the Publish button to take the new custom post live.

How to Create a Template and Fetching List

Creating a template and fetching a list requires creating a new file named template-news.php.

  • Place the newly created file in your theme folder.
  • Add the following code to the file:
Share