How to create a custom post type in WordPress?

How to create a custom post type in WordPress?


For certain websites such as e-commerce sites, the creation of custom post type is an interesting boon to vary the content and thus be able to stand out differently. Indeed, a custom post type that is to say a “custom content type” allows each site owner to be able to list content other than articles or web pages provided by default by WordPress.

What is a WordPress custom post type?

A WordPress custom post type is simply a “custom content type” to create and integrate into a website. It allows you to stand out from the content provided by default. Creating a custom post type can be done in two ways: using a plugin or manually by code. The WordPress platform basically offers two types of publications which are pages and articles.

However, the owner of a website, depending on his activity, may want to present other types of publications such as lists on his site. These lists generally take the form of a list of products, a list of upcoming events or even a list of terms.

He is possible to create a WordPress custom post type in two ways : first thanks to an extension or then manually by the code. If you have the choice, it is obviously much easier and faster to use an extension provided by WordPress. This will allow you to easily choose your publication settings via the dashboard. On the other hand, be careful, because the day you delete the extension, the custom post type creations will disappear with it.

See also  Better for custom app icons with Dynamic Island

How to create a WordPress custom post type?

Step 1: Add code to function.php file

First, start by opening your theme’s functions.php file (let’s take the Movies custom post type example here) and then copy and paste the code below:

register_post_type( ‘movies’,

Be careful, when you fill in the name of the CPT located in register_post_type, not to put accents or spaces. Inserting an accent or a space could cause errors.

Here are some examples of choices available to you:

  • hierarchical: this option allows you to obtain a parent/child link. For the category hierarchy, the equivalence is “true”. For the label hierarchy this is “false”
  • has_archive: This is to create archive pages for your custom post type. “false” is the default
  • public: this is to know if your custom post type is visible. “true” represents the default value
  • rewrite: to determine the slug used in the custom post type

In this example, Movies is used, but if you want to display a list of items for your store for example, just change the locations Movies to Items.

Step 2: Checking the display of the custom post type

Once you have completed step 1, remember to check in your WordPress dashboard that the type of publication you have just customized is displayed on the screen.

See also  How to create a poll on Instagram and its Stories?

custom post type wordpress

If the new category appears in the left sidebar, then you have successfully completed step 1.

Step 3: Customizing the new custom post type

The previous steps passed successfully, so you can introduce additional features with the code below:

 

/*

* Creating a function to create our CPT

*/

 

function custom_post_type() {

 

// Set UI labels for Custom Post Type

$labels = array(

'name' => _x( 'Movies', 'Post Type General Name', 'twentytwenty' ),

'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentytwenty' ),

'menu_name' => __( 'Movies', 'twentytwenty' ),

'parent_item_colon' => __( 'Parent Movie', 'twentytwenty' ),

'all_items' => __( 'All Movies', 'twentytwenty' ),

'view_item' => __( 'View Movie', 'twentytwenty' ),

'add_new_item' => __( 'Add New Movie', 'twentytwenty' ),

'add_new' => __( 'Add New', 'twentytwenty' ),

'edit_item' => __( 'Edit Movie', 'twentytwenty' ),

'update_item' => __( 'Update Movie', 'twentytwenty' ),

'search_items' => __( 'Search Movie', 'twentytwenty' ),

'not_found' => __( 'Not Found', 'twentytwenty' ),

'not_found_in_trash' => __( 'Not found in Trash', 'twentytwenty' ),

);

 

// Set other options for Custom Post Type

 

$args = array(

'label' => __( 'movies', 'twentytwenty' ),

'description' => __( 'Movie news and reviews', 'twentytwenty' ),

'labels' => $labels,

// Features this CPT supports in Post Editor

'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),

// You can associate this CPT with a taxonomy or custom taxonomy.

'taxonomies' => array( 'genres' ),

/* A hierarchical CPT is like Pages and can have

* Parent and child items. A non-hierarchical CPT

* is like Posts.

*/

'hierarchical' => false,

'public' => true,

'show_ui' => true,

'show_in_menu' => true,

'show_in_nav_menus' => true,

'show_in_admin_bar' => true,

'menu_position' => 5,

'can_export' => true,

'has_archive' => true,

'exclude_from_search' => false,

'publicly_queryable' => true,

'capability_type' => 'post',

'show_in_rest' => true,

 

);

 

// Registering your Custom Post Type

register_post_type( 'movies', $args );

 

}

 

/* Hook into the 'init' action so that the function

* Containing our post type registration is not

* unnecessarily executed.

*/

 

add_action( 'init', 'custom_post_type', 0 );

 

By doing so, it allows you to add custom fields as much as you want and also to be able to create parent/child category relationships to your custom post type. It is important to note that the name of the new custom post type cannot exceed 20 characters and must primarily consist of alphanumeric characters.

See also  6 ideas for newsletter unsubscribe forms

A custom post type is a great way to add differentiating content to your website and bring a personalized touch. It’s up to you to choose the way that suits you to create it.

HubSpot Community

To take your strategy a step further, download this free e-book and learn how to optimize a landing page for lead generation.How to Optimize a Landing Page for Lead Generation

Leave a Comment

Your email address will not be published.