Created
May 1, 2026 11:15
-
-
Save hmowais/b8e4f408bfb395edfe09d02c55755fc1 to your computer and use it in GitHub Desktop.
Updates [Custom Post Type with Taxonomy - 2026]
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
| <?php | |
| /* Register CPT */ | |
| /* https://developer.wordpress.org/resource/dashicons/ */ | |
| function cwptheme_post_type() { | |
| $post_types_arr = array( | |
| array( | |
| 'name' => 'Blogs', | |
| 'slug' => 'blogs', // URL slug | |
| 'icon' => 'dashicons-edit', // Admin menu icon (dashicons) | |
| 'menu_pos' => 5, // Admin menu position | |
| 'has_archive' => true, // Archive page enable/disable | |
| 'hierarchical'=> false, // true = page-like, false = post-like | |
| 'rewrite_slug'=> 'blogs', // Frontend URL rewrite slug | |
| ), | |
| array( | |
| 'name' => 'Leistungen', | |
| 'slug' => 'leistungen', | |
| 'icon' => 'dashicons-hammer', | |
| 'menu_pos' => 6, | |
| 'has_archive' => true, | |
| 'hierarchical'=> true, | |
| 'rewrite_slug'=> 'leistungen', | |
| ), | |
| array( | |
| 'name' => 'Unsere Leistungen', | |
| 'slug' => 'unsere-leistungen', | |
| 'icon' => 'dashicons-star-filled', | |
| 'menu_pos' => 7, | |
| 'has_archive' => true, | |
| 'hierarchical'=> true, | |
| 'rewrite_slug'=> 'unsere-leistungen', | |
| ), | |
| ); | |
| foreach ( $post_types_arr as $pt ) { | |
| $labels = array( | |
| 'name' => __( $pt['name'] ), | |
| 'singular_name' => __( $pt['name'] ), | |
| 'menu_name' => __( $pt['name'] ), | |
| 'parent_item_colon' => __( 'Parent ' . $pt['name'] ), | |
| 'all_items' => __( 'All ' . $pt['name'] ), | |
| 'view_item' => __( 'View ' . $pt['name'] ), | |
| 'add_new_item' => __( 'Add New '. $pt['name'] ), | |
| 'add_new' => __( 'Add New' ), | |
| 'edit_item' => __( 'Edit ' . $pt['name'] ), | |
| 'update_item' => __( 'Update ' . $pt['name'] ), | |
| 'search_items' => __( 'Search ' . $pt['name'] ), | |
| 'not_found' => __( 'Not Found' ), | |
| 'not_found_in_trash' => __( 'Not found in Trash' ), | |
| ); | |
| $args = array( | |
| 'label' => __( $pt['name'] ), | |
| 'description' => __( 'Add ' . $pt['name'] ), | |
| 'labels' => $labels, | |
| 'menu_icon' => $pt['icon'], // Dashicon ya custom image URL | |
| 'menu_position' => $pt['menu_pos'], // Admin sidebar position | |
| 'supports' => array( | |
| 'title', | |
| 'editor', | |
| 'excerpt', | |
| 'author', | |
| 'thumbnail', | |
| 'revisions', | |
| 'custom-fields', | |
| 'page-attributes', | |
| ), | |
| 'rewrite' => array( | |
| 'slug' => $pt['rewrite_slug'], // Frontend URL slug | |
| 'with_front' => false, // true = prefix with front base | |
| ), | |
| 'public' => true, | |
| 'hierarchical' => $pt['hierarchical'], | |
| 'show_ui' => true, | |
| 'show_in_menu' => true, | |
| 'show_in_nav_menus' => true, | |
| 'show_in_admin_bar' => true, | |
| 'show_in_rest' => true, // Gutenberg editor support | |
| 'has_archive' => $pt['has_archive'], | |
| 'can_export' => true, | |
| 'exclude_from_search' => false, | |
| 'yarpp_support' => true, | |
| 'publicly_queryable' => true, | |
| 'capability_type' => 'page', | |
| ); | |
| register_post_type( $pt['slug'], $args ); | |
| } | |
| } | |
| add_action( 'init', 'cwptheme_post_type', 0 ); | |
| /* Register Taxonomy */ | |
| function taxoFunc( $taxoName, $postType, $args_override = array() ) { | |
| $menuName = ucwords( $taxoName ); | |
| // Slug: lowercase, spaces to underscores | |
| $taxoSlug = strtolower( str_replace( ' ', '_', $taxoName ) ); | |
| $labels = array( | |
| 'name' => _x( $menuName, 'taxonomy general name' ), | |
| 'singular_name' => _x( $menuName, 'taxonomy singular name' ), | |
| 'search_items' => __( 'Search' ), | |
| 'all_items' => __( 'All' ), | |
| 'parent_item' => __( 'Parent' ), | |
| 'parent_item_colon' => __( 'Parent:' ), | |
| 'edit_item' => __( 'Edit' ), | |
| 'update_item' => __( 'Update' ), | |
| 'add_new_item' => __( 'Add New' ), | |
| 'new_item_name' => __( 'New Name' ), | |
| 'menu_name' => __( $menuName ), | |
| ); | |
| $defaults = array( | |
| 'hierarchical' => true, // true = category-like, false = tag-like | |
| 'labels' => $labels, | |
| 'show_ui' => true, | |
| 'show_in_rest' => true, // Gutenberg support | |
| 'show_admin_column' => true, // Post list mein column dikhao | |
| 'query_var' => true, | |
| 'rewrite' => array( | |
| 'slug' => $taxoSlug, | |
| 'with_front' => false, | |
| ), | |
| ); | |
| // Override defaults with custom args agar pass kiye hon | |
| $args = array_merge( $defaults, $args_override ); | |
| register_taxonomy( $taxoSlug, $postType, $args ); | |
| } | |
| // Usage — teesra parameter optional override array hai | |
| taxoFunc( 'Blogs Categories', 'blogs' ); | |
| taxoFunc( 'Leistungen Categories', 'leistungen' ); | |
| taxoFunc( 'Unsere Leistungen Categories','unsere-leistungen' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment