如何将这个将术语插入到自定义分类中的函数与另一个添加自定义分类筛选器的函数合并?

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

几天前我问了一个关于How to show a custom taxonomy in a custom post type column (and insert some \'default\' terms to the custom taxonomy).

然后我看到了答案Adding a Taxonomy Filter to Admin List for a Custom Post Type.

(两者answered 通过MikeSchinkel)

我想合并这两个功能:创建自定义帖子类型、自定义分类、在自定义分类中添加一些术语、在自定义帖子类型的列中显示自定义分类以及添加分类过滤器。

我知道这听起来很长,这两个问题都解决了,只需要把它们合并起来。

代码是什么样子的?

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

那么,你想要这个?

WordPress Admin for Custom Post Types with Custom Filters and Custom Columns
(来源:mikeschinkel.com)

好的,给你:

<?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\'),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(\'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);
     }
   }
   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;
   }
   function manage_posts_custom_column( $column,$post_id ) {
     global $typenow;
     if ($typenow==\'static_content\') {
       $taxonomy = \'section\';
       switch ($column) {
       case \'sections\':
         $sections = get_the_terms($post_id,$taxonomy);
         if (is_array($sections)) {
           foreach($sections as $key => $section) {
             $edit_link = get_term_link($section,$taxonomy);
             $sections[$key] = \'<a href="\'.$edit_link.\'">\' . $section->name . \'</a>\';
           }
           echo implode(\' | \',$sections);
         }
         break;
       }
     }
   }
   function parse_query($query) {
     global $pagenow;
     $qv = &$query->query_vars;
     if ($pagenow==\'edit.php\' &&
         isset($qv[\'taxonomy\']) && $qv[\'taxonomy\']==\'section\' &&
         isset($qv[\'term\']) && is_numeric($qv[\'term\'])) {
       $term = get_term_by(\'id\',$qv[\'term\'],\'section\');
       $qv[\'term\'] = $term->slug;
     }
   }
   function restrict_manage_posts() {
     global $typenow;
     global $wp_query;
     if ($typenow==\'static_content\') {
       $taxonomy = \'section\';
       $section = get_taxonomy($taxonomy);
       wp_dropdown_categories(array(
         \'show_option_all\' =>  __("Show All {$section->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_StaticContent::on_load();
}

SO网友:janoChen

瞧,我做到了(而且成功了):

有什么区别(哪种代码更好)?

I tried this (see the last function):**

    /**
     * Description: Adds a taxonomy filter in the admin list page for a custom post type.
     * Written for: http://wordpress.stackexchange.com/posts/582/
     * By: Mike Schinkel - http://mikeschinkel.com/custom-workpress-plugins
     * Instructions: Put this code in your theme\'s functions.php file or inside your own plugin. Edit to suite your post types and taxonomies. Hope this helps...
    */
    add_filter(\'manage_listing_posts_columns\', \'add_businesses_column_to_listing_list\');
    function add_businesses_column_to_listing_list( $posts_columns ) {
        if (!isset($posts_columns[\'author\'])) {
            $new_posts_columns = $posts_columns;
        } else {
            $new_posts_columns = array();
            $index = 0;
            foreach($posts_columns as $key => $posts_column) {
                if ($key==\'author\') {
                $new_posts_columns[\'businesses\'] = null;
                }
                $new_posts_columns[$key] = $posts_column;
            }
        }
        $new_posts_columns[\'businesses\'] = \'Businesses\';
        return $new_posts_columns;
    }

    add_action(\'manage_posts_custom_column\', \'show_businesses_column_for_listing_list\',10,2);
    function show_businesses_column_for_listing_list( $column_id,$post_id ) {
        global $typenow;
        if ($typenow==\'listing\') {
            $taxonomy = \'business\';
            switch ($column_id) {
            case \'businesses\':
                $businesses = get_the_terms($post_id,$taxonomy);
                if (is_array($businesses)) {
                    foreach($businesses as $key => $business) {
                        $edit_link = get_term_link($business,$taxonomy);
                        $businesses[$key] = \'<a href="\'.$edit_link.\'">\' . $business->name . \'</a>\';
                    }
                    echo implode(\' | \',$businesses);
                }
                break;
            }
        }
    }

    /* JUST AN HYPOTHETICAL EXAMPLE
    add_action(\'manage_posts_custom_column\', \'manage_posts_custom_column\',10,2);
    function manage_posts_custom_column( $column_id,$post_id ) {
        global $typenow;
        switch ("{$typenow}:{$column_id}") {
        case \'listing:business\':
            echo \'...whatever...\';
            break;
        case \'listing:property\':
            echo \'...whatever...\';
            break;
        case \'agent:listing\':
            echo \'...whatever...\';
            break;
        }
    }
    */

    add_action(\'restrict_manage_posts\',\'restrict_listings_by_business\');
    function restrict_listings_by_business() {
        global $typenow;
        global $wp_query;
        if ($typenow==\'listing\') {
            $taxonomy = \'business\';
            $business_taxonomy = get_taxonomy($taxonomy);
            wp_dropdown_categories(array(
                \'show_option_all\' =>  __("Show All {$business_taxonomy->label}"),
                \'taxonomy\'        =>  $taxonomy,
                \'name\'            =>  \'business\',
                \'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
            ));
        }
    }

    add_filter(\'parse_query\',\'convert_business_id_to_taxonomy_term_in_query\');
    function convert_business_id_to_taxonomy_term_in_query($query) {
        global $pagenow;
        $qv = &$query->query_vars;
        if ($pagenow==\'edit.php\' &&
                isset($qv[\'taxonomy\']) && $qv[\'taxonomy\']==\'business\' &&
                isset($qv[\'term\']) && is_numeric($qv[\'term\'])) {
            $term = get_term_by(\'id\',$qv[\'term\'],\'business\');
            $qv[\'term\'] = $term->slug;
        }
    }

    add_action(\'init\',\'register_listing_post_type\');
    function register_listing_post_type() {
        register_post_type(\'listing\',array(
            \'label\' => \'Listings\',
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'show_ui\' => true,
            \'query_var\' => true,
            \'rewrite\' => true,
            \'capability_type\' => \'post\',
            \'hierarchical\' => false,
        ));
    }

    add_action(\'init\',\'register_business_taxonomy\');
    function register_business_taxonomy() {
        register_taxonomy(\'business\',array(\'listing\'),array(
            \'label\' => \'Businesses\',
            \'public\'=>true,
            \'hierarchical\'=>true,
            \'show_ui\'=>true,
            \'query_var\'=>true
        ));
    }

    add_action(\'init\',\'insert_default_terms\');
    function insert_default_terms() {
      if (!get_option(\'site-page-content-initialized\')) {
        $terms = array(
          \'Intro (Front Page)\',
          \'Content (Front Page)\',
          \'Slider (Front Page)\',
          );
        foreach($terms as $term) {
          if (!get_term_by(\'name\',$term,\'business\')) {
            wp_insert_term($term, \'business\');
          }
        }
        update_option(\'site-page-content-initialized\',true);
      }
    }

结束

相关推荐