Last active
June 17, 2020 05:13
-
-
Save vanpariyar/dfe4eeea1dd89ac47f8712451ef70b6e to your computer and use it in GitHub Desktop.
Generate Post type dynamically in wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function customiser_init(){ | |
$post_types = array( | |
array( | |
'singular_name' => 'Financial', | |
'plural_name' => 'Financials', | |
'slug' => 'financials', | |
'post_type' => 'financials', | |
'support' => array( 'title', 'author' ), | |
), | |
); | |
foreach( $post_types as $key => $post_type) { | |
$labels = array( | |
'name' => _x( $post_type['plural_name'], 'Post type general name', 'divi' ), | |
'singular_name' => _x( $post_type['singular_name'], 'Post type singular name', 'divi' ), | |
'menu_name' => _x( $post_type['plural_name'], 'Admin Menu text', 'divi' ), | |
'name_admin_bar' => _x( $post_type['singular_name'], 'Add New on Toolbar', 'divi' ), | |
'add_new' => __( 'Add New', 'divi' ), | |
'add_new_item' => __( 'Add New '.$post_type['plural_name'], 'divi' ), | |
'new_item' => __( 'New '.$post_type['plural_name'], 'divi' ), | |
'edit_item' => __( 'Edit '.$post_type['plural_name'], 'divi' ), | |
'view_item' => __( 'View '.$post_type['plural_name'], 'divi' ), | |
'all_items' => __( 'All '.$post_type['plural_name'], 'divi' ), | |
'search_items' => __( 'Search '.$post_type['plural_name'], 'divi' ), | |
'parent_item_colon' => __( 'Parent '.$post_type['plural_name'], 'divi' ), | |
'not_found' => __( 'No '.$post_type['plural_name'].'found.', 'divi' ), | |
'not_found_in_trash' => __( 'No '.$post_type['plural_name'].'found in Trash.', 'divi' ), | |
'featured_image' => _x( $post_type['singular_name'].' Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'divi' ), | |
'set_featured_image' => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'divi' ), | |
'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'divi' ), | |
'use_featured_image' => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'divi' ), | |
'archives' => _x( $post_type['singular_name'].' archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'divi' ), | |
'insert_into_item' => _x( 'Insert into '.$post_type['plural_name'], 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'divi' ), | |
'uploaded_to_this_item' => _x( 'Uploaded to this '.$post_type['plural_name'], 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'divi' ), | |
'filter_items_list' => _x( 'Filter '.$post_type['plural_name'].'list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'divi' ), | |
'items_list_navigation' => _x( $post_type['plural_name'].'list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'divi' ), | |
'items_list' => _x( $post_type['plural_name'].'list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'divi' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => $post_type['singular_name'].' custom post type.', | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => $post_type['slug'], 'with_front' => true ), | |
'capability_type' => 'post', | |
'has_archive' => $post_type['slug'], | |
'hierarchical' => false, | |
'menu_position' => 20, | |
'supports' => $post_type['support'], | |
//'taxonomies' => array( 'years' ), | |
'show_in_rest' => true | |
); | |
register_post_type( $post_type['slug'], $args ); | |
} | |
/* | |
$labels = array( | |
'name' => _x( 'Years', 'taxonomy general name' ), | |
'singular_name' => _x( 'Year', 'taxonomy singular name' ), | |
'search_items' => __( 'Search Years' ), | |
'all_items' => __( 'All Years' ), | |
'parent_item' => __( 'Parent Year' ), | |
'parent_item_colon' => __( 'Parent Year:' ), | |
'edit_item' => __( 'Edit Year' ), | |
'update_item' => __( 'Update Year' ), | |
'add_new_item' => __( 'Add New Year' ), | |
'new_item_name' => __( 'New Year Name' ), | |
'menu_name' => __( 'Years' ), | |
); | |
// Now register the taxonomy | |
register_taxonomy('years',array('financials'), array( | |
'hierarchical' => true, | |
'labels' => $labels, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'years' ), | |
)); | |
*/ | |
add_rewrite_rule( | |
'financials/([0-9]{4})/([0-9]{1,2})/?$', | |
'index.php?post_type=financials&year=$matches[1]&monthnum=$matches[2]', | |
'top' | |
); | |
add_rewrite_rule( | |
'financials/([0-9]{4})/?$', | |
'index.php?post_type=financials&year=$matches[1]', | |
'top' | |
); | |
add_rewrite_rule( | |
'financials/([0-9]{4})/page/([0-9]{1,})/?$', | |
'index.php?post_type=financials&year=$matches[1]&paged=$matches[2]', | |
'top' | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment