Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Right now I use the following code to create a custom post type and assign a taxonomy to it:

// === CUSTOM TAXONOMIES === //
add_action('init', 'my_custom_taxonomies', 0);

function my_custom_taxonomies() {
 register_taxonomy(
  'section',  // internal name = machine-readable taxonomy name
  'static_content',  // object type = post, page, link, or custom post-type
  array(
   'hierarchical' => true,
   'labels' => array(
    'name' => __( 'Section' ),
    'singular_name' => __( 'Section' ),
    'add_new_item' => 'Add New Section',
    'edit_item' => 'Edit Section',
    'new_item' => 'New Section',
    'search_items' => 'Search Section',
    'not_found' => 'No Sections found',
    'not_found_in_trash' => 'No Sections found in trash',
    'all_items' => __( 'All Sections' ),
   ),
   'query_var' => true, // enable taxonomy-specific querying
   'rewrite' => array( 'slug' => 'section' ), // pretty permalinks for your taxonomy?
  )
 );
 wp_insert_term('Footer', 'section');
 wp_insert_term('Header', 'section');
 wp_insert_term('Front Page Intro', 'section');
 wp_insert_term('Front Page Content', 'section');
}

// === CUSTOM POST TYPES === //
add_action( 'init', 'create_my_post_types' );

function create_my_post_types() {
 register_post_type( 'static_content',
  array(
   'labels' => array(
    'name' => __( 'Static Content' ),
    'singular_name' => __( 'Static Content' ),
    'add_new_item' => 'Add New Static Content',
    'edit_item' => 'Edit Static Content',
    'new_item' => 'New Static Content',
    'search_items' => 'Search Static Content',
    'not_found' => 'No Static Content found',
    'not_found_in_trash' => 'No Static Content found in trash',
   ),
   '_builtin' => false,
   'public' => true,
   'hierarchical' => false,
   'taxonomies' => array( 'section'),
   'supports' => array(
    'title',
    'editor',
    'excerpt'
   ),
   'rewrite' => array( 'slug' => 'static_content', 'with_front' => false )
  )
 );
}

When I click the menu of the custom post type (in this case "Static Content"), I see in the table/column (where all the posts are displayed) the following information: Title, Author and Date. I would like to show the taxonomy assigned to that post (in this case the "Section".)

How do I do that?

share|improve this question
    
    
@t310s - I definitely don't want to appear contradictory but I feel like that other question was related but not identical. As such I'm going to help out @janoChen and answer it. –  MikeSchinkel Jan 23 '11 at 4:39
    
No problem, go for it.. :) ..and i've retracted my previous comment.. –  t31os Jan 23 '11 at 13:26
add comment

1 Answer 1

up vote 3 down vote accepted

Hi @janoChen:

Using my test site and some test code this is what I think you were looking for?

Screenshot of WordPress Custom Post List in the Admin showing associated Taxonomies

To achieve this you need to use two (2) hooks, one being specific to your post type: 'manage_static_content_posts_columns' and 'manage_posts_custom_column'. In order to make it work I took your code and packaged it in a class, and also made it a plugin (it doesn't have to be a plugin; you could just take the code and put in in your theme's functions.php file if you like.)

<?php 
/*
Plugin name: Static Content
*/
if (!class_exists('YourSite_StaticContent')) {
  class YourSite_StaticContent {
    static function on_load() {
      add_action('init',array(__CLASS__,'init'));
      add_filter('manage_static_content_posts_columns',
          array(__CLASS__,'manage_static_content_posts_columns'));
      add_filter('manage_posts_custom_column', 
          array(__CLASS__,'manage_posts_custom_column'));
    }
    function manage_static_content_posts_columns($columns){
      $new = array();
      foreach($columns as $key => $title) {
        if ($key=='author') // Put the Sections column before the Author column
          $new['sections'] = 'Sections';
        $new[$key] = $title;
      }
      return $new;
    }
    static function manage_posts_custom_column($column){
      global $post;
      switch ($column) {
        case "sections":
          echo get_the_term_list($post->ID, 'section', '', ', ','');
          break;
      }
    }
    static function init() {
      register_post_type('static_content',array(
        'labels' => array(
          'name' => __( 'Static Content' ),
          'singular_name' => __( 'Static Content' ),
          'add_new_item' => 'Add New Static Content',
          'edit_item' => 'Edit Static Content',
          'new_item' => 'New Static Content',
          'search_items' => 'Search Static Content',
          'not_found' => 'No Static Content found',
          'not_found_in_trash' => 'No Static Content found in trash',
        ),
        'public' => true,
        'hierarchical' => false,
        'taxonomies' => array( 'section'),
        'supports' => array('title','editor','excerpt'),
        'rewrite' => array('slug'=>'static_content','with_front'=>false),
      ));
      register_taxonomy('section','static_content',array(
        'hierarchical' => true,
        'labels' => array(
          'name' => __( 'Section' ),
          'singular_name' => __( 'Section' ),
          'add_new_item' => 'Add New Section',
          'edit_item' => 'Edit Section',
          'new_item' => 'New Section',
          'search_items' => 'Search Section',
          'not_found' => 'No Sections found',
          'not_found_in_trash' => 'No Sections found in trash',
          'all_items' => __( 'All Sections' ),
        ),
        'query_var' => true,
        'rewrite' => array( 'slug' => 'section' ),
        ));
      if (!get_option('yoursite-static-content-initialized')) {
        $terms = array(
          'Footer',
          'Header',
          'Front Page Intro',
          'Front Page Content',
          );
        foreach($terms as $term) {
          if (!get_term_by('name',$term,'section')) {
            wp_insert_term($term, 'section');
          }
        }
        update_option('yoursite-static-content-initialized',true);
      }
    }
  }
  YourSite_StaticContent::on_load();
}
share|improve this answer
    
Works perfectly! Thanks. (By the way did you do something similar to this with your client?) –  janoChen Jan 23 '11 at 6:48
    
@janoChen - Thanks, and glad I could help. Yes I did similar on mandslaw.com. It is pretty much (in my opinion) a standard "must have" for custom post types; each type has a set of data that is relevant to it and the "standard" list isn't that useful in many cases. –  MikeSchinkel Jan 23 '11 at 8:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.