Skip to content

Instantly share code, notes, and snippets.

@B1-0S
Last active April 18, 2018 19:47
Show Gist options
  • Save B1-0S/7ef5b5c1c98165236a8b5805fdd4831d to your computer and use it in GitHub Desktop.
Save B1-0S/7ef5b5c1c98165236a8b5805fdd4831d to your computer and use it in GitHub Desktop.
WordPress: Add Custom Post Type Product and Products Categories Taxonomy
<?php
/**
* Register product custom post type
* @return void
*/
function alcolock_product_cpt() {
$labels = array(
'name' => _x( 'Products', 'Post Type General Name', 'alcolock' ),
'singular_name' => _x( 'Product', 'Post Type Singular Name', 'alcolock' ),
'menu_name' => __( 'Products', 'alcolock' ),
'name_admin_bar' => __( 'Products', 'alcolock' ),
'archives' => __( 'Product Archives', 'alcolock' ),
'attributes' => __( 'Product Attributes', 'alcolock' ),
'parent_item_colon' => __( 'Parent Product:', 'alcolock' ),
'all_items' => __( 'All Products', 'alcolock' ),
'add_new_item' => __( 'Add New Product', 'alcolock' ),
'add_new' => __( 'Add Product', 'alcolock' ),
'new_item' => __( 'New Product', 'alcolock' ),
'edit_item' => __( 'Edit Product', 'alcolock' ),
'update_item' => __( 'Update Product', 'alcolock' ),
'view_item' => __( 'View Product', 'alcolock' ),
'view_items' => __( 'View Products', 'alcolock' ),
'search_items' => __( 'Search Product', 'alcolock' ),
'not_found' => __( 'Not found', 'alcolock' ),
'not_found_in_trash' => __( 'Not found in Trash', 'alcolock' ),
'featured_image' => __( 'Featured Image', 'alcolock' ),
'set_featured_image' => __( 'Set featured image', 'alcolock' ),
'remove_featured_image' => __( 'Remove featured image', 'alcolock' ),
'use_featured_image' => __( 'Use as featured image', 'alcolock' ),
'insert_into_item' => __( 'Insert into item', 'alcolock' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'alcolock' ),
'items_list' => __( 'Items list', 'alcolock' ),
'items_list_navigation' => __( 'Items list navigation', 'alcolock' ),
'filter_items_list' => __( 'Filter items list', 'alcolock' ),
);
$args = array(
'label' => __( 'Product', 'alcolock' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'taxonomies' => array( 'product_category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-tag',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => false,
);
register_post_type( 'product', $args );
}
add_action( 'init', 'alcolock_product_cpt', 0 );
/**
* Register Products Custom Taxonomy
* @return void
*/
function alcolock_product_taxonomy() {
$labels = array(
'name' => _x( 'Products Categories', 'Taxonomy General Name', 'alcolock' ),
'singular_name' => _x( 'Products Category', 'Taxonomy Singular Name', 'alcolock' ),
'menu_name' => __( 'Product Categories', 'alcolock' ),
'all_items' => __( 'All Categories', 'alcolock' ),
'parent_item' => __( 'Parent Category', 'alcolock' ),
'parent_item_colon' => __( 'Parent Category:', 'alcolock' ),
'new_item_name' => __( 'New Category Name', 'alcolock' ),
'add_new_item' => __( 'Add New Category', 'alcolock' ),
'edit_item' => __( 'Edit Category', 'alcolock' ),
'update_item' => __( 'Update Category', 'alcolock' ),
'view_item' => __( 'View Category', 'alcolock' ),
'separate_items_with_commas' => __( 'Separate categories with commas', 'alcolock' ),
'add_or_remove_items' => __( 'Add or remove Categories', 'alcolock' ),
'choose_from_most_used' => __( 'Choose from the most used', 'alcolock' ),
'popular_items' => __( 'Popular Categories', 'alcolock' ),
'search_items' => __( 'Search categories', 'alcolock' ),
'not_found' => __( 'Not Found', 'alcolock' ),
'no_terms' => __( 'No items', 'alcolock' ),
'items_list' => __( 'Items list', 'alcolock' ),
'items_list_navigation' => __( 'Items list navigation', 'alcolock' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'product_category', array( 'product' ), $args );
}
add_action( 'init', 'alcolock_product_taxonomy', 0 );

Simple Products WP CPT and Category

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment