如何在自定义帖子类型的表中显示分配给帖子的分类(其中显示了所有帖子)?

时间:2011-01-22 作者:janoChen

现在,我使用以下代码创建自定义帖子类型并为其分配分类法:

// === 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 )
  )
 );
}
当我单击自定义帖子类型(在本例中为“静态内容”)的菜单时,我会在表/列(显示所有帖子的位置)中看到以下信息:标题、作者和日期。我想展示分配给该帖子的分类法(在本例中为“部分”)

我该怎么做?

1 个回复
最合适的回答,由SO网友:MikeSchinkel 整理而成

使用我的测试站点和一些测试代码,这就是我认为您正在寻找的?

Screenshot of WordPress Custom Post List in the Admin showing associated Taxonomies
(来源:mikeschinkel.com)

要实现这一点,您需要使用两(2)个挂钩,其中一个针对您的帖子类型:\'manage_static_content_posts_columns\'\'manage_posts_custom_column\'. 为了让它工作起来,我把你的代码打包成一个类,还把它做成了一个插件(它不一定是插件;你可以把代码放进你的主题functions.php 如果愿意,请存档。)

<?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();
}

结束

相关推荐