下拉筛选器不检索自定义分类

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

Wordpress 3.0.4:

enter image description here

Wordpress 3.1 RC4:

enter image description here

functions.php (The last part is the one involved in filtering and displaying the drop-down menu):

<?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\'));
        }
        // Register custom post types and custom taxonomies
        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\' ),
            ));
            // Add initial terms
            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);
            }
        }
        //Arrange the position of elements in the table
        function manage_page_content_posts_columns($columns) {
            $columns = array(
                \'cb\' => \'<input type="checkbox" />\',
                \'title\' => \'Name\',
                \'locations_column\' => \'Locations\',
                \'page_sections_column\' => \'Page Sections\',
                \'author\' => \'Author\',
                \'date\' => \'Date\',
            );
            return $columns;
        }
        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();
}
有什么建议吗?

1 个回复
SO网友:helgatheviking

是不是因为您没有向parse\\u查询过滤器返回值?或者通过引用传递。

function parse_query(&$query) {  //added the reference &
            global $pagenow;
            $qv = &$query->query_vars;
            if ($pagenow==\'edit.php\' &&
                isset($qv[\'taxonomy\']) && $qv[\'taxonomy\']==\'page_sections\' &&
                isset($qv[\'term\']) && is_numeric($qv[\'term\']) &&$qv[\'term\']>0) { //added check for greater than 0 as the default ALL is zero and that throws some warnings
                    $term = get_term_by(\'id\',$qv[\'term\'],\'page_sections\');
                    $qv[\'term\'] = $term->slug;
                }
        }
除此之外,这与我本周末刚刚使用的代码非常相似:Adding a Taxonomy Filter to Admin List for a Custom Post Type?

结束

相关推荐