将第二个自定义分类添加到自定义帖子类型(两者都显示在其表中)?

时间:2011-02-06 作者:janoChen

以下代码由提供Mike Schinkel, 创建自定义帖子类型和自定义分类法。它使分类显示在自定义帖子类型的列中。最后,它插入了一些默认术语。

我想为自定义帖子类型(页面内容)插入第二个分类法,名为:Locations.

(第二个自定义分类法也应显示在自定义帖子类型的列中)

我如何修改代码来存档它?

<?php

/**
 * Create the Page Content custom post type and the Page Section custom taxonomy
 */
add_action(\'init\', \'my_init_method\');

if (!class_exists(\'YourSite_PageContent\')) {
 class YourSite_PageContent {
   static function on_load() {
     add_action(\'init\',array(__CLASS__,\'init\'));
     add_filter(\'manage_page_content_posts_columns\',
         array(__CLASS__,\'manage_page_content_posts_columns\'));
     add_filter(\'manage_posts_custom_column\',
         array(__CLASS__,\'manage_posts_custom_column\'),10,2);
     add_action(\'restrict_manage_posts\',
         array(__CLASS__,\'restrict_manage_posts\'));
     add_filter(\'parse_query\',
         array(__CLASS__,\'parse_query\'));
   }
   static function init() {
     register_post_type(\'page_content\',array(
       \'labels\' => array(
         \'name\' => __( \'Page Content\' ),
         \'singular_name\' => __( \'Page Content\' ),
         \'add_new_item\' => \'Add New Page Content\',
         \'edit_item\' => \'Edit Page Content\',
         \'new_item\' => \'New Page Content\',
         \'search_items\' => \'Search Page Content\',
         \'not_found\' => \'No Page Content found\',
         \'not_found_in_trash\' => \'No Page Content found in trash\',
       ),
       \'public\' => true,
       \'hierarchical\' => false,
       \'taxonomies\' => array( \'page_sections\'),
       \'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'custom-fields\'),
       \'rewrite\' => array(\'slug\'=>\'page_content\',\'with_front\'=>false),
     ));
     register_taxonomy(\'page_sections\',\'page_content\',array(
       \'hierarchical\' => true,
       \'labels\' => array(
         \'name\' => __( \'Page Sections\' ),
         \'singular_name\' => __( \'Page Section\' ),
         \'add_new_item\' => \'Add New Page Section\',
         \'edit_item\' => \'Edit Page Section\',
         \'new_item\' => \'New Page Section\',
         \'search_items\' => \'Search Page Section\',
         \'not_found\' => \'No Page Sections found\',
         \'not_found_in_trash\' => \'No Page Sections found in trash\',
         \'all_items\' => __( \'All Page Sections\' ),
       ),
       \'query_var\' => true,
       \'rewrite\' => array( \'slug\' => \'page_sections\' ),
       ));
     if (!get_option(\'yoursite-page-content-initialized\')) {
       $terms = array(
         \'Footer\',
         \'Header\',
         \'Front Page Intro\',
         \'Front Page Content\',
         );
       foreach($terms as $term) {
         if (!get_term_by(\'name\',$term,\'page_sections\')) {
           wp_insert_term($term, \'page_sections\');
         }
       }
       update_option(\'yoursite-page-content-initialized\',true);
     }
   }
   function manage_page_content_posts_columns($columns){
     $new = array();
     foreach($columns as $key => $title) {
       if ($key==\'author\') // Put the Page Sections column before the Author column
         $new[\'page_sections_column\'] = \'Page Sections\';
       $new[$key] = $title;
     }
     return $new;
   }
   function manage_posts_custom_column( $column,$post_id ) {
     global $typenow;
     if ($typenow==\'page_content\') {
       $taxonomy = \'page_sections\';
       switch ($column) {
       case \'page_sections_column\':
         $page_sections_column = get_the_terms($post_id,$taxonomy);
         if (is_array($page_sections_column)) {
           foreach($page_sections_column as $key => $page_sections) {
             $edit_link = get_term_link($page_sections,$taxonomy);
             $page_sections_column[$key] = \'<a href="\'.$edit_link.\'">\' . $page_sections->name . \'</a>\';
           }
           echo implode(\' | \',$page_sections_column);
         }
         break;
       }
     }
   }
   function parse_query($query) {
     global $pagenow;
     $qv = &$query->query_vars;
     if ($pagenow==\'edit.php\' &&
         isset($qv[\'taxonomy\']) && $qv[\'taxonomy\']==\'page_sections\' &&
         isset($qv[\'term\']) && is_numeric($qv[\'term\'])) {
       $term = get_term_by(\'id\',$qv[\'term\'],\'page_sections\');
       $qv[\'term\'] = $term->slug;
     }
   }
   function restrict_manage_posts() {
     global $typenow;
     global $wp_query;
     if ($typenow==\'page_content\') {
       $taxonomy = \'page_sections\';
       $page_sections = get_taxonomy($taxonomy);
       wp_dropdown_categories(array(
         \'show_option_all\' =>  __("Show All {$page_sections->label}"),
         \'taxonomy\'        =>  $taxonomy,
         \'name\'            =>  $taxonomy,
         \'orderby\'         =>  \'name\',
         \'selected\'        =>  $wp_query->query[\'term\'],
         \'hierarchical\'    =>  true,
         \'depth\'           =>  3,
         \'show_count\'      =>  true,  // This will give a view
         \'hide_empty\'      =>  true,   // This will give false positives, i.e. one\'s not empty related to the other terms. TODO: Fix that
       ));
     }
   }
 }
 YourSite_PageContent::on_load();
}
enter image description here

EDIT (What I\'ve done so far with no PHP skills):

通过修改代码,到目前为止我已经做到了:

<?php

/**
 * Create the Page Content custom post type and the Page Section custom taxonomy
 */
add_action(\'init\', \'my_init_method\');

if (!class_exists(\'YourSite_PageContent\')) {
 class YourSite_PageContent {
   static function on_load() {
     add_action(\'init\',array(__CLASS__,\'init\'));
     add_filter(\'manage_page_content_posts_columns\',
         array(__CLASS__,\'manage_page_content_posts_columns\'));
     add_filter(\'manage_posts_custom_column\',
         array(__CLASS__,\'manage_posts_custom_column\'),10,2);
     add_action(\'restrict_manage_posts\',
         array(__CLASS__,\'restrict_manage_posts\'));
     add_filter(\'parse_query\',
         array(__CLASS__,\'parse_query\'));
   }
   static function init() {
     register_post_type(\'page_content\',array(
       \'labels\' => array(
         \'name\' => __( \'Page Content\' ),
         \'singular_name\' => __( \'Page Content\' ),
         \'add_new_item\' => \'Add New Page Content\',
         \'edit_item\' => \'Edit Page Content\',
         \'new_item\' => \'New Page Content\',
         \'search_items\' => \'Search Page Content\',
         \'not_found\' => \'No Page Content found\',
         \'not_found_in_trash\' => \'No Page Content found in trash\',
       ),
       \'public\' => true,
       \'hierarchical\' => false,
       \'taxonomies\' => array( \'page_sections\'),
       \'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'custom-fields\'),
       \'rewrite\' => array(\'slug\'=>\'page_content\',\'with_front\'=>false),
     ));
     register_taxonomy(\'locations\',\'page_content\',array(
       \'hierarchical\' => true,
       \'labels\' => array(
         \'name\' => __( \'Locations\' ),
         \'singular_name\' => __( \'Page Section\' ),
         \'add_new_item\' => \'Add New Page Section\',
         \'edit_item\' => \'Edit Page Section\',
         \'new_item\' => \'New Page Section\',
         \'search_items\' => \'Search Page Section\',
         \'not_found\' => \'No Page Sections found\',
         \'not_found_in_trash\' => \'No Page Sections found in trash\',
         \'all_items\' => __( \'All Page Sections\' ),
       ),
       \'query_var\' => true,
       \'rewrite\' => array( \'slug\' => \'locations\' ),
       ));
     register_taxonomy(\'page_sections\',\'page_content\',array(
       \'hierarchical\' => true,
       \'labels\' => array(
         \'name\' => __( \'Page Sections\' ),
         \'singular_name\' => __( \'Page Section\' ),
         \'add_new_item\' => \'Add New Page Section\',
         \'edit_item\' => \'Edit Page Section\',
         \'new_item\' => \'New Page Section\',
         \'search_items\' => \'Search Page Section\',
         \'not_found\' => \'No Page Sections found\',
         \'not_found_in_trash\' => \'No Page Sections found in trash\',
         \'all_items\' => __( \'All Page Sections\' ),
       ),
       \'query_var\' => true,
       \'rewrite\' => array( \'slug\' => \'page_sections\' ),
       ));
     if (!get_option(\'yoursite-page-content-initialized\')) {
       $terms = array(
         \'Footer\',
         \'Header\',
         \'Front Page Intro\',
         \'Front Page Content\',
         );
       foreach($terms as $term) {
         if (!get_term_by(\'name\',$term,\'page_sections\')) {
           wp_insert_term($term, \'page_sections\');
         }
       }
       update_option(\'yoursite-page-content-initialized\',true);
     }
   }
   function manage_page_content_posts_columns($columns){
     $new = array();
     foreach($columns as $key => $title) {
       if ($key==\'author\') // Put the Page Sections column before the Author column
         $new[\'page_sections_column\'] = \'Page Sections\';
       $new[$key] = $title;
     }
     return $new;
   }
   function manage_posts_custom_column( $column,$post_id ) {
     global $typenow;
     if ($typenow==\'page_content\') {
       $locations_taxonomy = \'locations\';
       $page_sections_taxonomy = \'page_sections\';
       switch ($column) {
       case \'locations_column\':
         $locations_column = get_the_terms($post_id,$locations_taxonomy);
         if (is_array($locations_column)) {
           foreach($locations_column as $key => $locations) {
             $edit_link = get_term_link($locations,$locations_taxonomy);
             $locations_column[$key] = \'<a href="\'.$edit_link.\'">\' . $locations->name . \'</a>\';
           }
           echo implode(\' | \',$locations_column);
         }
         break;
       case \'page_sections_column\':
         $page_sections_column = get_the_terms($post_id,$page_sections_taxonomy);
         if (is_array($page_sections_column)) {
           foreach($page_sections_column as $key => $page_sections) {
             $edit_link = get_term_link($page_sections,$page_sections_taxonomy);
             $page_sections_column[$key] = \'<a href="\'.$edit_link.\'">\' . $page_sections->name . \'</a>\';
           }
           echo implode(\' | \',$page_sections_column);
         }
         break;
       }
     }
   }
   function parse_query($query) {
     global $pagenow;
     $qv = &$query->query_vars;
     if ($pagenow==\'edit.php\' &&
         isset($qv[\'taxonomy\']) && $qv[\'taxonomy\']==\'page_sections\' &&
         isset($qv[\'term\']) && is_numeric($qv[\'term\'])) {
       $term = get_term_by(\'id\',$qv[\'term\'],\'page_sections\');
       $qv[\'term\'] = $term->slug;
     }
   }
   function restrict_manage_posts() {
     global $typenow;
     global $wp_query;
     if ($typenow==\'page_content\') {
       $taxonomy = \'page_sections\';
       $page_sections = get_taxonomy($taxonomy);
       wp_dropdown_categories(array(
         \'show_option_all\' =>  __("Show All {$page_sections->label}"),
         \'taxonomy\'        =>  $taxonomy,
         \'name\'            =>  $taxonomy,
         \'orderby\'         =>  \'name\',
         \'selected\'        =>  $wp_query->query[\'term\'],
         \'hierarchical\'    =>  true,
         \'depth\'           =>  3,
         \'show_count\'      =>  true,  // This will give a view
         \'hide_empty\'      =>  true,   // This will give false positives, i.e. one\'s not empty related to the other terms. TODO: Fix that
       ));
     }
   }
 }
 YourSite_PageContent::on_load();
}
enter image description here

I\'m still struggling with this line:

   function manage_page_content_posts_columns($columns){
     $new = array();
     foreach($columns as $key => $title) {
       if ($key==\'author\') // Put the Page Sections column before the Author column
         $new[\'locations_column\'] = \'Locations\';
         $new[\'cb\'] = \'<input type="checkbox">\';
         $new[\'title\'] = \'Title\';
         $new[\'page_sections_column\'] = \'Page Sections\';
       $new[$key] = $title;
     }
     return $new;
   }

I can\'t manage to place Page Sections before Locations.

如何解决这个问题?

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

我在过滤器挂钩中使用以下内容manage_edit-<post-type>_columns

add_filter( \'manage_edit-visitor_columns\', array( &$this, \'_wp_filter_visitor_columns\' ) );

public function _wp_filter_visitor_columns( $columns )
{
    $columns = array(
            \'cb\' => \'<input type="checkbox" />\',
            \'title\' => \'Name\',
            \'type\' => \'Type\',
            //\'state\' => \'Current State\',
            \'id\' => \'Visitor ID\',
            \'acct\' => \'Using Account\',
        );

    return $columns;
}
为了过滤列视图,我使用以下方法:

add_action( \'manage_posts_custom_column\', array( &$this, \'_wp_filter_visitor_column_view\' ) );

public function _wp_filter_visitor_column_view( $column )
{
    global $post;
    if ( $column == "type" )
    {
        //$terms = get_the_term_list( $post->ID, \'v_types\', \'\', \', \', \'\' );
        //echo strip_tags( $terms );

        $terms = wp_get_object_terms( $post->ID, \'v_types\' );
        echo $terms[0]->name;
    }
    elseif ( $column == "state" OR $column == "workstation" )
    {
        $workstation = get_post_meta( $post->ID, \'v_workstation\', true );
        if ( $column == "workstation" ) echo $workstation;
        elseif ( $column == "state" ) echo \'Not coded....\';
    }
    elseif ( $column == "id" )
    {
        echo get_post_meta( $post->ID, \'v_id\', true );
    }
    elseif ( $column == "acct" )
    {
        $terms = wp_get_object_terms( $post->ID, \'v_accounts\' );
        echo $terms[0]->name;
    }
}
我希望这对你有帮助。

结束

相关推荐