Let’s Create a Custom Post Type !

Here we shall create a custom post type which will display portfolio. Lets started.

  • Register Your Custom Function.

    Open your function.php of current theme. At the end of the function file, type the following line of code to execute the custom function named create_portfolio during the initialization phase every time a page is generated.

     add_action( 'init', 'create_movie_review' ); 
  • Function Implementation

    Provide an implementation of the create_portfolio function.

     function create_movie_review() {
        register_post_type( 'portfolio',
            array(
                'labels' => array(
                    'name' => 'Portfolio',
                    'singular_name' => 'Portfolio',
                    'add_new' => 'Add New',
                    'add_new_item' => 'Add New Portfolio',
                    'edit' => 'Edit',
                    'edit_item' => 'Edit Portfolio',
                    'new_item' => 'New Portfolio',
                    'view' => 'View',
                    'view_item' => 'View Portfolio',
                    'search_items' => 'Search Portfolio',
                    'not_found' => 'No Portfolio found',
                    'not_found_in_trash' => 'No Portfolio found in Trash',
                    'parent' => 'Parent Portfolio'
                ),
                'public' => true,
                'menu_position' => 25,
                'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
                'taxonomies' => array( '' ),
                'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
                'has_archive' => true
            )
        );
    } 

    The

    register_post_type

    function does most of the work for us. As soon as it is called it prepares the WordPress environment for a new custom post type including a different sections in wordpress admin. This function takes two arguments: first one is an unique name of the custom post type and the second one an array demonstrating the properties of the new custom post type. Here it’s another array containing the different labels, which indicates the text strings to be displayed in the different sections of the custom post type e.g. ‘name‘ displays the custom post type name in the dashboard, ‘edit‘ and ‘view‘ are displayed in Edit and View buttons respectively.

    In the second arguments:

    • 'public' => true

      determines the visibility of the custom post type both in the admin panel and front end.

    • 'menu_position' => 25

      determines the menu position of the custom post type.

    • 'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' )

      determines the features of the custom post type which is to be displayed.

    • 'taxonomies' => array( '' )

      creates custom taxonomies.

    • 'menu_icon' => ''

      put the image url which is displaying in admin.

    • 'has_archive' => true

      enables/disable archive of the custom post type.

    For more details please visit the WordPress Codex

    register_post_type

    page

  • Now save the function file and go to your wordpress admin.
  • Add new item

    Click on the Add New link to go to the custom post type editor. Provide a portfolio title, a small description and set the featured image.

  • Publish

    Publish the portfolio and click on View Portfolio to view the created portfolio in the browser.