lucbord
2/5/2016 - 5:18 PM

multiple custom post types registration

multiple custom post types registration

<?php

class FREE_postypes {

    public function __construct() {
        add_action( 'init', array( $this, 'all_post_types' ) );
    }

    public function all_post_types() {
        
        $post_types = [
            
            [
                'post_type'     => 'Staff',
                'singular'      => 'staff',
                'slug'          => 'staff',
                'menu_icon'     => 'dashicons-portfolio',

            ],
            [
                'post_type'     => 'Terapie',
                'singular'      => 'terapia',
                'slug'          => 'Terapie',
                'menu_icon'     => 'dashicons-universal-access'
            ],


        ];

        foreach ($post_types as $key => $post_type) { $this -> free_register_post_type( $post_type ); }
    }
    

    private function free_register_post_type( $data ) {

        $singular  = $data['singular'];
        $plural    = $data['post_type'];;
        $post_type = $data['post_type'];
        $slug      = $data['slug'];
        $icon      = $data['menu_icon'];

        $labels = array(
            'name'               => _x( $plural, 'post type general name'  ),
            'singular_name'      => _x( $singular, 'post type singular name'  ),
            'menu_name'          => _x( $plural, 'admin menu'  ),
            'name_admin_bar'     => _x( $singular, 'add new on admin bar'  ),
            'add_new'            => _x( 'Add New', $singular  ),
            'add_new_item'       => __( 'Add New ' . $singular  ),
            'new_item'           => __( 'New ' . $singular  ),
            'edit_item'          => __( 'Edit ' . $singular  ),
            'view_item'          => __( 'View ' . $singular  ),
            'all_items'          => __( 'All ' . $plural  ),
            'search_items'       => __( 'Search ' . $plural  ),
            'parent_item_colon'  => __( 'Parent ' . $plural . ':'  ),
            'not_found'          => __( 'No ' . $plural . ' found.'  ),
            'not_found_in_trash' => __( 'No ' . $plural . ' found in Trash.'  )
        );

        $args = array(
            'labels'             => $labels,
            'description'        => __( $singular .'.'  ),
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'show_in_rest'       => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => $slug ),
            'capability_type'    => 'post',
            'has_archive'        => true,
            'hierarchical'       => false,
            'menu_position'      => 6,
            'menu_icon'          => $icon,
            'supports'           => array( 'title', 'editor', 'thumbnail' ,'custom-fields')
        );

        register_post_type( $post_type, $args );

    }


} 

new FREE_postypes;