Custom post typeΒΆ

Wordpress had wide range of funcitonalities. However, sometimes we need to enhance this. One of the best way to do so is creating the custom post type. The following creates the custom post Porfolio type, which intends to hold our company porfolios.

<?php
function my_custom_post_porfolio() {
    $labels = array(
        'name'               => _x( 'Porfolio', 'post type general name' ),
        'singular_name'      => _x( 'Porfolio', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'Porfolio' ),
        'add_new_item'       => __( 'Add New Porfolio' ),
        'edit_item'          => __( 'Edit Porfolio' ),
        'new_item'           => __( 'New Porfolio' ),
        'all_items'          => __( 'All Porfolios' ),
        'view_item'          => __( 'View Porfolio' ),
        'search_items'       => __( 'Search Porfolios' ),
        'not_found'          => __( 'No Porfolios found' ),
        'not_found_in_trash' => __( 'No Porfolios found in the Trash' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'Porfolios'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our Porfolios data',
        'public'        => true,
        'menu_position' => 5,
        'has_archive'   => true,
        'rewrite' => array('slug' => 'porfolio'),
    );
    register_post_type( 'porfolio', $args );
}
add_action( 'init', 'my_custom_post_porfolio' );

Usually, we will need to add some new fields to the newly-created post type, we can do it by using the CMB2 plugin:

<?php
function cmb2_porfolio_metaboxes() {
        $cmb = new_cmb2_box(array(
                'id'            => 'post_porfolio',
                'title'         => __( 'Test Metabox', 'cmb2' ),
                'object_types'  => array( 'porfolio', ), // important!
                'context'       => 'normal',
                'priority'      => 'high',
                'show_names'    => true, // Show field names on the left
        ) );
        map_fields($cmb, Porfolio::get_fields());
}