Created
March 14, 2012 18:06
-
-
Save adamcbrewer/2038286 to your computer and use it in GitHub Desktop.
WP: Custom Taxonomies And Post Types
This file contains 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 //http://codex.wordpress.org/Function_Reference/register_post_type#Example | |
/** | |
* Base-settings for adding additional features to an existing WP install | |
* | |
* Location: functions.php file | |
* | |
* | |
* | |
* It's best to play around with the setting below, but this | |
* basic structure allows for: | |
* | |
* 1. custom post types (Pages & Posts) | |
* 2. custom taxonomies (Tags & Categories) | |
* 3. adding custom taxonomies to existing post types | |
* 4. adding featured-images to posts | |
* 5. fixing the permalinks for custom post types/taxonomies | |
*/ | |
/********************************************************* | |
CUSTOM FUNCTIONS | |
**********************************************************/ | |
// enable 'feature-image' thumbnails on posts | |
if ( function_exists( 'add_theme_support' ) ) { | |
add_theme_support( 'post-thumbnails' ); | |
} | |
/** | |
* Custom Post Types | |
* | |
* Add additional Post & Page types | |
* | |
* @author Adam Brewer | |
*/ | |
add_action( 'init', 'my_custom_post_types' ); | |
function my_custom_post_types() { | |
register_post_type( 'games', | |
array( | |
'labels' => array( | |
'name' => __( 'Games' ), | |
'singular_name' => __( 'Game' ), | |
'add_new' => 'Add New Game', | |
'edit_item' => 'Edit Game', | |
'new_item' => 'New Game', | |
'view_item' => 'View Games', | |
'search_items' => 'Search Games', | |
'not_found' => 'No Games found', | |
'not_found_in_trash' => 'No Games found in Trash' | |
), | |
'public' => true, | |
'has_archive' => true, | |
'rewrite' => array('slug' => 'games'), | |
'supports' => array( 'title','editor', 'thumbnail', 'excerpt', 'custom-fields' ), | |
'taxonomies' => array('skill') // hard-set the allowed taxonomies, built in (tags, categories) or custom ones (skills) | |
) | |
); | |
} | |
/** | |
* Custom Taxonomies | |
* | |
* Add additional Categories & Post Tags types | |
* | |
* @author Adam Brewer | |
*/ | |
add_action( 'init', 'create_custom_taxonomies', 0 ); | |
function create_custom_taxonomies() { | |
// New taxonomy: Skills [non hierarchical] | |
$labels = array( | |
'name' => _x( 'Skills', 'taxonomy general name' ), | |
'singular_name' => _x( 'Skill', 'taxonomy singular name' ), | |
'search_items' => __( 'Search Skills' ), | |
'all_items' => __( 'All Skills' ), | |
'parent_item' => NULL, | |
'parent_item_colon' => NULL, | |
'edit_item' => __( 'Edit Skill' ), | |
'update_item' => __( 'Update Skill' ), | |
'add_new_item' => __( 'Add New Skill' ), | |
'new_item_name' => __( 'New Skill Name' ), | |
'menu_name' => __( 'Skills' ), | |
); | |
register_taxonomy('skill', array('game'), array( | |
'hierarchical' => true, | |
'labels' => $labels, | |
'show_ui' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'skill' ), | |
)); | |
} | |
/** | |
* Register Custom Taxonomies to existing Post & Page types | |
* | |
* Only for custom tags & cats to existing Post & Page post types | |
* | |
* @author Adam Brewer | |
*/ | |
// add_action('init', 'my_add_tags_and_cats'); | |
function my_add_tags_and_cats() { | |
register_taxonomy_for_object_type('skill', 'games'); | |
} | |
/** | |
* Helper to fix the permalink re-writes issue with custom post types | |
* | |
* @author http://xplus3.net/2010/05/20/wp3-custom-post-type-permalinks/ | |
*/ | |
function my_post_type_link_filter_function( $post_link, $id = 0, $leavename = FALSE ) { | |
if ( strpos('%issue_project%', $post_link) === FALSE ) { | |
return $post_link; | |
} | |
$post = get_post($id); | |
if ( !is_object($post) || $post->post_type != 'issue' ) { | |
return $post_link; | |
} | |
$terms = wp_get_object_terms($post->ID, 'issue_project'); | |
if ( !$terms ) { | |
return str_replace('project/%issue_project%/', '', $post_link); | |
} | |
return str_replace('%issue_project%', $terms[0]->slug, $post_link); | |
} | |
add_filter('post_type_link', 'my_post_type_link_filter_function', 1, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment