Friday, February 24, 2017

Add tag and category to custom post type

Hi.
Here i share simple code for create tag and category for custom post type. Put this code in functions.php in your theme folder



//hook into the init action and call create_Category_nonhierarchical_taxonomy when it fires

add_action( 'init', 'create_Category_nonhierarchical_taxonomy', 0 );

function create_Category_nonhierarchical_taxonomy() {

// Labels part for the GUI

  $labels = array(
    'name' => _x( 'Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Category' ),
    'popular_items' => __( 'Popular Category' ),
    'all_items' => __( 'All Category' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Category' ),
    'update_item' => __( 'Update Category' ),
    'add_new_item' => __( 'Add New Category' ),
    'new_item_name' => __( 'New Category Name' ),
    'separate_items_with_commas' => __( 'Separate Category with commas' ),
    'add_or_remove_items' => __( 'Add or remove Category' ),
    'choose_from_most_used' => __( 'Choose from the most used Category' ),
    'menu_name' => __( 'Category' ),
  );

// Now register the non-hierarchical taxonomy like tag

  register_taxonomy('RCategory','resources',array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'rcategory' ),
  ));
  register_taxonomy(
            'resources_tag',
            'resources',
            array(
                'hierarchical'  => false,
                'label'         => __( 'Tags', CURRENT_THEME ),
                'singular_name' => __( 'Tag', CURRENT_THEME ),
                'rewrite'       => true,
                'query_var'     => true
            ) 
        );
}
//end code

No comments:

Post a Comment