Skip to content

Instantly share code, notes, and snippets.

@anwerashif
Created October 28, 2017 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anwerashif/7b39cbf69f3996ed766d95b9d6405f4f to your computer and use it in GitHub Desktop.
Save anwerashif/7b39cbf69f3996ed766d95b9d6405f4f to your computer and use it in GitHub Desktop.
Register 'custom' post type to WordPress
<?php
// Do NOT include the opening PHP tag
/**
* Register `code` post type
*/
function code_post_type() {
// Labels
$labels = array(
'name' => _x("Code Snippets", "post type general name"),
'singular_name' => _x("Code Snippet", "post type singular name"),
'menu_name' => 'Code Snippets',
'add_new' => _x("Add New Snippet", "code item"),
'add_new_item' => __("Add New Snippet"),
'edit_item' => __("Edit Snippet"),
'new_item' => __("New Snippet"),
'view_item' => __("View Snippet"),
'search_items' => __("Search Snippets"),
'not_found' => __("No Snippets Found"),
'not_found_in_trash' => __("No Snippets Found in Trash"),
'parent_item_colon' => ''
);
// Register post type
register_post_type('code' , array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-media-code',
'rewrite' => array('slug' => 'code'),
'supports' => array('title', 'editor'),
'menu_position' => 5
) );
}
add_action( 'init', 'code_post_type', 0 );
/**
* Register `Code Language` taxonomy
*/
function code_taxonomy() {
// Labels
$singular = 'Language';
$plural = 'Languages';
$labels = array(
'name' => _x( $plural, "taxonomy general name"),
'singular_name' => _x( $singular, "taxonomy singular name"),
'search_items' => __("Search $singular"),
'all_items' => __("All $singular"),
'parent_item' => __("Parent $singular"),
'parent_item_colon' => __("Parent $singular:"),
'edit_item' => __("Edit $singular"),
'update_item' => __("Update $singular"),
'add_new_item' => __("Add New $singular"),
'new_item_name' => __("New $singular Name"),
);
// Register and attach to 'code' post type
register_taxonomy( strtolower($singular), 'code', array(
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'hierarchical' => true,
'query_var' => true,
'rewrite' => false,
'labels' => $labels
) );
}
add_action( 'init', 'code_taxonomy', 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment