{"id":4211,"date":"2016-05-30T02:13:42","date_gmt":"2016-05-30T02:13:42","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4211"},"modified":"2016-05-30T02:13:42","modified_gmt":"2016-05-30T02:13:42","slug":"wordpress-register-taxonomy-example-register_taxonomy","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/wordpress-register-taxonomy-example-register_taxonomy\/","title":{"rendered":"WordPress: Register Taxonomy Example (register_taxonomy)"},"content":{"rendered":"<p>If you create a new Post type in WordPress, it&#8217;s often useful to add a custom Taxonomy (similar to categories \/ tags on Posts). In this case, we&#8217;re adding an &#8220;error message&#8221; type (that contains lots of lists of error messages), and adding a &#8220;library&#8221; taxonomy, for which Javascript library the error comes from.<\/p>\n<p>To do this, you first need to register the new type, in functions.php:<\/p>\n<pre lang=\"php\">\nadd_action( 'init', 'create_post_type' );\nfunction create_post_type() {\n  register_post_type( 'error_message',\n    array(\n      'labels' => array(\n        'name' => __( 'Errors' ),\n        'singular_name' => __( 'Error' )\n      ),\n      'public' => true,\n      'has_archive' => true,\n    )\n  );\n\n  ...\n}\n<\/pre>\n<p>When you customize WordPress, it seems to be quite sensitive about you making labels for all the spots you might use the thing.<\/p>\n<p>So, before we add the taxonomy, we have to write labels for everything:<\/p>\n<pre lang=\"php\">\n$labels = array(\n  'name'              => _x( 'Libraries', 'taxonomy general name' ),\n  'singular_name'     => _x( 'Library', 'taxonomy singular name' ),\n  'search_items'      => __( 'Search Libraries' ),\n  'all_items'         => __( 'All Libraries' ),\n  'parent_item'       => __( 'Parent Library' ),\n  'parent_item_colon' => __( 'Parent Library:' ),\n  'edit_item'         => __( 'Edit Library' ),\n  'update_item'       => __( 'Update Library' ),\n  'add_new_item'      => __( 'Add New Library' ),\n  'new_item_name'     => __( 'New Library Name' ),\n  'menu_name'         => __( 'Library' ),\n);\n<\/pre>\n<p>Then, finally we can create the taxonomy (note the slug is important &#8211; this determines URLs in use):<\/p>\n<pre lang=\"php\">\n$args = array(\n  'hierarchical'      => false,\n  'labels'            => $labels,\n  'show_ui'           => true,\n  'show_admin_column' => true,\n  'query_var'         => true,\n  'rewrite'           => array( 'slug' => 'library' ),\n);\n\nregister_taxonomy( 'library_category', 'error_message', $args );\n<\/pre>\n<p><img alt='' class='alignnone size-full wp-image-4213' src='http:\/\/172.104.26.128\/wp-content\/uploads\/2016\/05\/img_574ba1a2c05d0.png' \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to add custom taxonomies to Wordpress<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[432,601],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4211"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=4211"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4211\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}