Skip to content

Instantly share code, notes, and snippets.

@marcosfreitas
Created March 10, 2015 02:57
Show Gist options
  • Save marcosfreitas/7b90df3af2afe952a6b8 to your computer and use it in GitHub Desktop.
Save marcosfreitas/7b90df3af2afe952a6b8 to your computer and use it in GitHub Desktop.
Registering Taxonomies and Post_type - WordPress
// Register Custom Post Type
function c4n_new_post_type() {
$labels = array(
'name' => _x( 'Journals', 'Post Type General Name', 'turing_press' ),
'singular_name' => _x( 'Journal', 'Post Type Singular Name', 'turing_press' ),
'menu_name' => __( 'Journals', 'turing_press' ),
'parent_item_colon' => __( 'Parent Journal:', 'turing_press' ),
'all_items' => __( 'All Journals', 'turing_press' ),
'view_item' => __( 'View Journal', 'turing_press' ),
'add_new_item' => __( 'Add New Journal', 'turing_press' ),
'add_new' => __( 'Add New', 'turing_press' ),
'edit_item' => __( 'Edit Journal', 'turing_press' ),
'update_item' => __( 'Update Journal', 'turing_press' ),
'search_items' => __( 'Search Journal', 'turing_press' ),
'not_found' => __( 'Not found', 'turing_press' ),
'not_found_in_trash' => __( 'Not found in Trash', 'turing_press' ),
);
// create a new taxonomy like categories in post
$category_labels = array(
'name' => _x( 'Category', 'Taxonomy General Name', 'turing_press' ),
'singular_name' => _x( 'Category', 'Taxonomy Singular Name', 'turing_press' ),
'menu_name' => __( 'Categories', 'turing_press' ),
'all_items' => __( 'All Items', 'turing_press' ),
'parent_item' => __( 'Parent Item', 'turing_press' ),
'parent_item_colon' => __( 'Parent Item:', 'turing_press' ),
'new_item_name' => __( 'New Category', 'turing_press' ),
'add_new_item' => __( 'Add New Item', 'turing_press' ),
'edit_item' => __( 'Edit Item', 'turing_press' ),
'update_item' => __( 'Update Item', 'turing_press' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'turing_press' ),
'search_items' => __( 'Search Items', 'turing_press' ),
'add_or_remove_items' => __( 'Add or remove items', 'turing_press' ),
'choose_from_most_used' => __( 'Choose from the most used items', 'turing_press' ),
'not_found' => __( 'Not Found', 'turing_press' ),
);
$category_rewrite = array(
'slug' => 'categories',
'with_front' => false, // to not show the category like a base of permalink
'hierarchical' => false,
);
$category_args = array(
'labels' => $category_labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $category_rewrite,
);
register_taxonomy( 'journal_category', array( 'journal' ), $category_args );
// create a custom taxonomy like category in post
$edition_labels = array(
'name' => _x( 'Edition', 'Taxonomy General Name', 'turing_press' ),
'singular_name' => _x( 'Edition', 'Taxonomy Singular Name', 'turing_press' ),
'menu_name' => __( 'Editions', 'turing_press' ),
'all_items' => __( 'All Items', 'turing_press' ),
'parent_item' => __( 'Parent Item', 'turing_press' ),
'parent_item_colon' => __( 'Parent Item:', 'turing_press' ),
'new_item_name' => __( 'New Edition', 'turing_press' ),
'add_new_item' => __( 'Add New Item', 'turing_press' ),
'edit_item' => __( 'Edit Item', 'turing_press' ),
'update_item' => __( 'Update Item', 'turing_press' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'turing_press' ),
'search_items' => __( 'Search Items', 'turing_press' ),
'add_or_remove_items' => __( 'Add or remove items', 'turing_press' ),
'choose_from_most_used' => __( 'Choose from the most used items', 'turing_press' ),
'not_found' => __( 'Not Found', 'turing_press' ),
);
$edition_rewrite = array(
'slug' => 'edition',
'with_front' => true,
'hierarchical' => false,
);
$edition_args = array(
'labels' => $edition_labels,
'hierarchical' => true, // beahvior like categories
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $edition_rewrite,
);
register_taxonomy( 'journal_edition', array( 'journal' ), $edition_args );
// continue register post_type
$args = array(
'label' => __( 'journal', 'turing_press' ),
'description' => __( 'Posts of Journals', 'turing_press' ),
'labels' => $labels,
'supports' => array( 'title', 'thumbnail', 'revisions'),
'taxonomies' => array( 'journal_category', 'journal_edition' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-aside',
'can_export' => true,
'has_archive' => true, // if false, will be removed from the breadcrumbs
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'journal', $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment