Creating a Custom Post Type (CPT) in Astratic theme

WordPress has started as a simple blogging platform but treating it the same way now would be a big understatement. It is a robust Content Management System (CMS) that goes far beyond posts and articles. One way to discover its full potential is by using a custom post type.

What is a custom post type?

Let’s imagine that you want to start a platform with high school reviews. You want to collect facilities from the neighborhood providing some useful data like:

  • the average of the high school diploma results
  • the number of students who qualify for the provincial level of the olympiad in given subjects
  • additional classes (like programming or Spanish)
  • students reviews
  • parents reviews
  • the number of tutors with a Ph.D. title.

To create such kind of library you can use CPT. It enables the ability to build a template for any kind of post and makes possible ordering them in new taxonomy (e.g. by districts or cities in the region). Interested? Let’s dive in!

How to create a CPT?

Let’s assume that we want to publish school review on the website using Custom Post Type (CPT). We start by finding the functions.php file in the directory with the Astratic template. Finally, paste the code, remembering to replace the labels and other arguments according to your needs.

function job_post_type() {
    $labels = array(
        'name'                => 'School review',
        'singular_name'       => 'School review',
        'menu_name'           => 'School review',
        'parent_item_colon'   => 'School review',
        'all_items'           => 'All schools',
        'view_item'           => 'See review',
        'add_new_item'        => 'Add school review',
        'add_new'             => 'Add new school',
        'edit_item'           => 'Edit school',
        'update_item'         => 'Update',
        'search_items'        => 'Search reviews',
        'not_found'           => 'Not found',
        'not_found_in_trash'  => 'Not found'
    ); 
    $args = array(
        'label' => 'school',
        'rewrite' => array(
            'slug' => 'school-reviews'
        ),
        'description'         => 'School review',
        'labels'              => $labels,
        'supports'            => array( 'title', 'thumbnail'),
        'taxonomies'          => array(),
        'hierarchical'        => false,
        'public'              => true, 
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 4,
        'menu_icon'           => 'dashicons-id-alt',
        'can_export'          => true,
        'has_archive'         => false,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
    );
    register_post_type( 'school review', $args );
} 
add_action( 'init', 'job_post_type', 0 );

As you can see, register_post_type takes a lot of arguments. Fortunately, in most cases it is enough to change a few basic ones:

  • label – a unique name like “post”, you will use it even when creating queries via WP Query,
  • rewrite – allows you to change the URL address where “school” entries will be available,
  • supports – by adding or removing arguments in array, you can define post’s functionality with features like ‘editor’ which enables a post content editor function, or ‘thumbnail’ which enables post’s thumbnail functionality,
  • menu_icon – icon in the administrator menu.

Send the file to the server, if everything went well, a new item will appear in the menu.

If you are interested in building own page templates read our article. We also send other useful tutorials from WPBeginner and Cloudways.

Do you have any questions? We are here to help! 🙂

Add the first comment and start a discussion

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses cookies to optimize the use of it and for statistical purposes in accordance with Privacy Policy. The conditions for storing cookie files can be specified in Browser Settings. Further using the site and not blocking cookies means that you accept the use of these files.

Service namePurposeSettings
Cookies funkcyjneCookies required for the operation of the website.
Google AnalyticsAnalytical purposes.
DisqusCookies required for the operation of the comments system.

Need help?

Thank you!

success

We will answer in next 24 hours

Need a help? support-icon