I found this post while sorting through my old drafts and decided to go ahead and publish it rather than trashing it. Hopefully the code samples don’t break too badly in WordPress 3.9.
I’ve talked a bit about when to use custom taxonomies and when to use custom fields/post meta (and how they can be used virtually interchangeably in some situations). If you want to use taxonomies, you’ll probably also want to:
- make sure that the terms you want to use exist in your custom taxonomy
- limit the ability for these terms to be altered
This Gist is a good start:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // check for a term and insert it if it doesn't exist | |
| if (!get_term_by('slug', $term_slug, $taxonomy)) { | |
| wp_insert_term(__($term_name, 'localization-key'), $taxonomy, array( | |
| 'slug' => $term_slug | |
| )); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // remove all functionality from hierarchical taxonomy UI box (other than term selection) | |
| // *and* convert the checkboxes to radio buttons to enforce single selection (this is untested) | |
| $('#taxonomy-' + yourTaxonomyName) | |
| .find('.category-tabs, div:not(".tabs-panel")').remove().end() | |
| .find('.tabs-panel').removeClass('tabs-panel').end() | |
| .find('input[type="checkbox"]').attr('type', 'radio'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // remove all functionality from hierarchical taxonomy UI box (other than term selection) | |
| $('#taxonomy-' + yourTaxonomyName) | |
| .find('.category-tabs, div:not(".tabs-panel")').remove().end() | |
| .find('.tabs-panel').removeClass('tabs-panel'); |
You’ll probably also want to specify:
'public' => false,
'show_ui' => true,
when defining your custom taxonomy. This makes the UI box for the taxonomy appear in the post/page editing interface as expected, but hides the admin forms for editing the taxonomy terms from the admin menu.
Alex King: Custom Taxonomy as “Post Meta” http://t.co/yi44ipOjPm
#WordPress Alex King: Custom Taxonomy as “Post Meta” http://t.co/aZGLgl67a2
Alex King: Custom Taxonomy as “Post Meta” http://t.co/RQMQ97h4hs #cms
Alex King: Custom Taxonomy as “Post Meta” http://t.co/ens5YmN9S2 #wordpress #wp
Alex King: Custom Taxonomy as “Post Meta” –
I found this post while sorting through my old drafts and decided t… http://t.co/yxjZFy93CP
Alex King: Custom Taxonomy as “Post Meta” http://t.co/r4yRD7vHCQ #php #wordpress
: Alex King: Custom Taxonomy as “Post Meta”, http://t.co/W7t8y2YaHc #WordPress
Alex King: Custom Taxonomy as “Post Meta”: I found this post while sorting through my old drafts and decided t… http://t.co/L0Iltnedf7
New on @alexkingorg: Custom Taxonomy as “Post Meta” http://t.co/bH8jsEsl0r
[…] Advanced stuff. Tuts+ explains how to display post meta data on a WordPress post. While we’re on the topic of post meta data, developer Alex King has also posted about custom taxonomy. […]
[…] Advanced stuff. Tuts+ explains how to display post meta data on a WordPress post. While we’re on the topic of post meta data, developer Alex King has also posted about custom taxonomy. […]