Codesnipp.it Social Code Sharing

Adam Shen

Fully functional wp3.0 texonomy with admin columns

by Adam Shen on Aug 11, 2010

add_action('init', 'rb_type_init', 0); function rb_type_init() { $args = array( 'labels' => array( 'name' => _x('Showcases', 'post type general name'), 'singular_name' => _x('Showcase', 'post type singular name'), 'add_new' => _x('Add New', 'book'), 'add_new_item' => __('Add New Case'), 'edit_item' => __('Edit Case'), 'new_item' => __('New Case'), 'view_item' => __('View Case'), 'search_items' => __('Search Cases'), 'not_found' => __('No case found'), 'not_found_in_trash' => __('No case found in Trash'), 'parent_item_colon' => '' ), 'public' => true, 'rewrite' => true, 'capability_type' => 'post', 'exclude_from_search' => false, 'hierarchical' => false, 'menu_position' => 5, 'taxonomies' => array('client','brand'), 'supports' => array('title','editor','thumbnail','excerpt','custom-fields','revisions') ); register_post_type('showcase',$args); // Add new taxonomy, make it hierarchical (like categories) $labels = array( 'name' => _x( 'Clients', 'taxonomy general name' ), 'singular_name' => _x( 'Client', 'taxonomy singular name' ), 'search_items' => __( 'Search Clients' ), 'popular_items' => __( 'Popular Clients' ), 'all_items' => __( 'All Clients' ), 'edit_item' => __( 'Edit Client' ), 'parent_item' => __( 'Parent Client' ), 'parent_item_colon' => __( 'Parent Client:' ), 'update_item' => __( 'Update Client' ), 'add_new_item' => __( 'Add New Client' ), 'new_item_name' => __( 'New Client Name' ), 'separate_items_with_commas' => __( 'Separate clients with commas' ), 'add_or_remove_items' => __( 'Add or remove clients' ), 'choose_from_most_used' => __( 'Choose from the most used clients' ) ); register_taxonomy('client','showcase',array( 'hierarchical' => true, 'labels' => $labels, 'public' => true, 'rewrite' => array( 'slug' => 'clients' ), )); // Add new taxonomy, NOT hierarchical (like tags) $labels = array( 'name' => _x( 'Brands', 'taxonomy general name' ), 'singular_name' => _x( 'Brand', 'taxonomy singular name' ), 'search_items' => __( 'Search Brands' ), 'all_items' => __( 'All Brands' ), 'parent_item' => null, 'parent_item_colon' => null, 'edit_item' => __( 'Edit Brand' ), 'update_item' => __( 'Update Brand' ), 'add_new_item' => __( 'Add New Brand' ), 'new_item_name' => __( 'New Brand Name' ), ); register_taxonomy('brand','showcase', array( 'hierarchical' => false, 'labels' => $labels, 'public' => true, 'rewrite' => array( 'slug' => 'brand' ), )); }; // add to admin columns function showcase_columns($posts_columns) { $posts_columns = array(); $posts_columns['cb'] = '<input type="checkbox" />'; $posts_columns['title'] = _x('Title', 'column name'); // $posts_columns['author'] = __('Author'); if ( empty($post_type) || is_object_in_taxonomy($post_type, 'category') ) $posts_columns['client_categories'] = __('Client'); if ( empty($post_type) || is_object_in_taxonomy($post_type, 'post_tag') ) $posts_columns['brand_tags'] = __('Brand'); $post_status = !empty($_REQUEST['post_status']) ? $_REQUEST['post_status'] : 'all'; if ( !in_array( $post_status, array('pending', 'draft', 'future') ) && ( empty($post_type) || post_type_supports($post_type, 'comments') ) ) $posts_columns['comments'] = '<div class="vers"><img alt="Comments" src="' . esc_url( admin_url( 'images/comment-grey-bubble.png' ) ) . '" /></div>'; $posts_columns['date'] = __('Date'); return $posts_columns; } function showcase_data_columns( $column_name) { switch ($column_name) { case 'client_categories': $_taxonomy = 'client'; $categories = get_the_terms( $post_id, $_taxonomy ); if ( !empty( $categories ) ) { $out = array(); foreach ( $categories as $c ) $out[] = "<a href='edit.php?client=$c->slug&post_type=product'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'category', 'display')) . "</a>"; echo join( ', ', $out ); } else { _e('Uncategorized'); } break; case 'brand_tags': $_taxonomy = 'brand'; $tags = get_the_terms( $post_id, $_taxonomy ); if ( !empty( $tags ) ) { $out = array(); foreach ( $tags as $c ) $out[] = "<a href='edit.php?brand=$c->slug&post_type=product'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'post_type', 'display')) . "</a>"; echo join( ', ', $out ); } else { _e(' No Tags'); } break; } } add_filter( 'manage_showcase_posts_columns', 'showcase_columns' ); add_action( 'manage_posts_custom_column', 'showcase_data_columns', 10, 2 ); //add filter to insure the text Showcases, or Showcase, is displayed when user updates a 'post' add_filter('post_updated_messages', 'case_updated_messages'); function case_updated_messages( $messages ) { $messages['showcase'] = array( 0 => '', // Unused. Messages start at index 1. 1 => sprintf( __('Case updated. <a href="%s">View case</a>'), esc_url( get_permalink($post_ID) ) ), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Case updated.'), /* translators: %s: date and time of the revision */ 5 => isset($_GET['revision']) ? sprintf( __('Case restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => sprintf( __('Case published. <a href="%s">View case</a>'), esc_url( get_permalink($post_ID) ) ), 7 => __('Case saved.'), 8 => sprintf( __('Case submitted. <a target="_blank" href="%s">Preview case</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), 9 => sprintf( __('Case scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview case</a>'), // translators: Publish box date format, see http://php.net/date date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), 10 => sprintf( __('Case draft updated. <a target="_blank" href="%s">Preview case</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), ); return $messages; }

Can't see the comments? Please login first :)