themes

How to Create a Custom Post Type with Taxonomy in WordPress

Learn how to create a custom post type with a custom taxonomy in WordPress. This step-by-step guide covers registering both using code, organizing your content efficiently, and following WordPress best practices.

/* Start projects post type*/   
function qrs_projects_posttype() {
    $labels = array(
            'name'               => _x( 'Projects', 'qrs' ),
            'singular_name'      => _x( 'Projects', 'qrs' ),
            'add_new'            => _x( 'Add New', 'qrs' ),
            'add_new_item'       => __( 'Add New Project' ),
            'edit_item'          => __( 'Edit Project' ),
            'new_item'           => __( 'New Project' ),
            'all_items'          => __( 'All Projects' ),
            'view_item'          => __( 'View Projects' ),
            'search_items'       => __( 'Search Projects' ),
            'not_found'          => __( 'No Projects found' ),
            'not_found_in_trash' => __( 'No Projects found in the Trash' ),
            'menu_name'          => 'Projects'
          ); 
    register_post_type( 'Projects',
        array(
            'labels' => $labels,
            'public' => true,
            'has_archive' => false,
            'supports'    => array( 'title','editor','thumbnail'),
            'rewrite' => array('slug' => 'Projects'),
            'show_in_rest' => true,
        )
    );
     
    
}
add_action( 'init', 'qrs_projects_posttype' );

/* start register taxonomy for type*/
function qrs_type_taxonomies() {
    register_taxonomy('type', 'projects', array(
    'hierarchical' => true,
    'labels' => array(
      'name' => _x( 'Type', 'qrs' ),
      'singular_name' => _x( 'Type', 'qrs' ),
      'search_items' =>  __( 'Search Type' ),
      'all_items' => __( 'All Type' ),
      'parent_item' => __( 'Parent Type' ),
      'parent_item_colon' => __( 'Parent Type:' ),
      'edit_item' => __( 'Edit Type' ),
      'update_item' => __( 'Update Type' ),
      'add_new_item' => __( 'Add New Type' ),
      'new_item_name' => __( 'New Type Name' ),
      'menu_name' => __( 'Type' )
    ),
    // Control the slugs used for this taxonomy
    'rewrite' => array(
      'slug' => 'type',
      'with_front' => false,
      'hierarchical' => true 
    )
  ));
}
add_action( 'init', 'qrs_type_taxonomies', 0 );
/* End register taxonomy for type*/

Leave a reply

Your email address will not be published. Required fields are marked *