最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I merge this function(s) that inserts terms to a custom taxonomy with this other that adds a custom taxonomy filter?

programmeradmin0浏览0评论

Few days ago I asked a question about How to show a custom taxonomy in a custom post type column (and insert some 'default' terms to the custom taxonomy).

Then I saw an answer for Adding a Taxonomy Filter to Admin List for a Custom Post Type.

(Both answered by MikeSchinkel)

I would like to merge both features: to create a custom post type, a custom taxonomy, adding some terms in the custom taxonomy, displaying the custom taxonomy in the custom post type's column, and adding a taxonomy filter.

I know it sounds long, they are all done in those two questions, just need to merge them.

How that code would look like?

Few days ago I asked a question about How to show a custom taxonomy in a custom post type column (and insert some 'default' terms to the custom taxonomy).

Then I saw an answer for Adding a Taxonomy Filter to Admin List for a Custom Post Type.

(Both answered by MikeSchinkel)

I would like to merge both features: to create a custom post type, a custom taxonomy, adding some terms in the custom taxonomy, displaying the custom taxonomy in the custom post type's column, and adding a taxonomy filter.

I know it sounds long, they are all done in those two questions, just need to merge them.

How that code would look like?

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Jan 24, 2011 at 8:06 wycwyc 3,90719 gold badges61 silver badges97 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

So, you want this?


(source: mikeschinkel)

Alright, here ya go:

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

Look I did this (and it worked):

What's the difference (which one is better code)?

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/posts/582/
     * By: Mike Schinkel - http://mikeschinkel/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);
      }
    }
发布评论

评论列表(0)

  1. 暂无评论