Hi guys,

In this article we aim to teach you how to create custom post types in WordPress. The true power of expanding wordpress beyond blogging is in the custom post types, which can be used in any form for countless types of sites or data. For example you want to display your favourite movies in your blogging website and keep them organised. See below an intruction what a post type is, and what a custom post type is:

First of all, what are Custom Post Types in WordPress?

WordPress comes by default with some post types available to users:

  1. Post
  2. Page
  3. Attachment
  4. Revision
  5. Nav menu
  6. Custom CSS
  7. Changeset

Therefore, you can create your own custom post types and give them whatever name you want. I will write briefly about each one so you can quickly understand the use of them.

Posts

The “post” is the WordPress Post Type you’ll use the most if you are interested in creating a blog. Also, they are displayed in reverse order, meaning the newest one will be shown first.

Pages

While “Posts” are dynamic content, “Pages” are static and are used to provide information that doesn’t change very often. They can be accessed from the navigation menu.

Attachments

“Attachments” are the uploads to your WordPress website and can be images, videos, pdfs or any kind of file.

Revisions

“Revisions” are a WordPress post type used to keep a history of the post types you created in case you want to correct a mistake or go back to a previous version. Posts can have multiple revisions which you can edit.

Menus

“Menus” are a WordPress post type used to create links so visitors can navigate your website to different locations.

Custom CSS

“Custom CSS” are a post type used to customize the appearance of a WordPress website.

Changesets

“Changesets” are like “Revisions” but for the customizer of the pages. Therefore, when you create some changes using the customizer, the pages will be saved as “drafts”.

Custom Post Types

“Custom Post Types” are used in case you want to have various categories on your site. For example, if you want to have a different section for Music it is recommended to create a custom post type for it.

There are two methods to create a custom post type: by using a plugin or manually.

Creating a custom post type with the help of a plugin is easier and recommended for beginners, but they disappear when the plugin is deactivated.

Plugins may slow the speed of the website, so if you do not want to install one, you can create custom post types manually by adding the code in your functions.php file. Or create a separate file just for this, and do not forget to include it into your functions.php file. Below the necessary code:

 


function sitemile_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 );

}

/*Add to the init hook */

add_action( ‘init’, ‘sitemile_custom_post_type’, 0 );

 

This entry was posted in Blog, Informative and tagged , , , . Bookmark the permalink.

Leave a Reply