未注册的分类仍显示为emtpy筛选器列表

时间:2018-09-22 作者:wittich

unregistered 我正在使用的主题的自定义分类:

function wpse314876_unregister_taxonomy() {
    unregister_taxonomy_for_object_type( \'page_category\', \'page\' );
}
add_action( \'init\', \'wpse314876_unregister_taxonomy\' );
但在所有页面概述中(/wp-admin/edit.php?post_type=page) 它仍然显示一个空的下拉筛选器列表<我可以remove all category drop-down 但这不是我想要的,因为我已经added a custom drop-down taxonomy filter-list.

下拉列表显示为一个小的空列表empty list 其HTML如下所示:

<select name="page_category" id="page_category" class="postform">
</select>

如何删除空下拉列表?

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

在我进行了一点挖掘之后,我在家长主题中找到了正确的行动。函数看起来是这样的(实际上正是示例Ilinked 以上内容):

//////////////////////////////
// Add page category filter //
//////////////////////////////

add_action(\'restrict_manage_posts\', \'uncode_page_filter_post_type_by_taxonomy\');

function uncode_page_filter_post_type_by_taxonomy() {
    global $typenow;
    $post_type = \'page\'; // change to your post type
    $taxonomy  = \'page_category\'; // change to your taxonomy
    if ($typenow == $post_type) {
        $selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : \'\';
        $info_taxonomy = get_taxonomy($taxonomy);
        wp_dropdown_categories(array(
            \'show_option_all\' => sprintf(esc_html__("Show All %s", \'uncode\'), $info_taxonomy->label),
            \'taxonomy\'        => $taxonomy,
            \'name\'            => $taxonomy,
            \'orderby\'         => \'name\',
            \'selected\'        => $selected,
            \'show_count\'      => true,
            \'hide_empty\'      => true,
        ));
    };
}

add_filter(\'parse_query\', \'uncode_page_convert_id_to_term_in_query\');
function uncode_page_convert_id_to_term_in_query($query) {
    global $pagenow;
    $post_type = \'page\'; // change to your post type
    $taxonomy  = \'page_category\'; // change to your taxonomy
    $q_vars    = &$query->query_vars;
    if ( $pagenow == \'edit.php\' && isset($q_vars[\'post_type\']) && $q_vars[\'post_type\'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
        $term = get_term_by(\'id\', $q_vars[$taxonomy], $taxonomy);
        $q_vars[$taxonomy] = $term->slug;
    }
}
要删除它们,我添加了一个简单的remove_action() 到我的孩子主题function.php.

/**
 * Remove page_category filter and parse query
 *
 **/
remove_action(\'restrict_manage_posts\', \'uncode_page_filter_post_type_by_taxonomy\');
remove_action(\'parse_query\', \'uncode_page_convert_id_to_term_in_query\');

结束

相关推荐

Taxonomy question

我的php非常有限。我编辑现有的东西以使其工作,通常可以或多或少地理解代码在做什么,但我自己无法编写代码。我会尽力解释我的情况。我有一个Woocommerce插件,它将产品显示为列表(在表中),而不是单独的框。此表有一个单元格,用于表示产品的“名称”。我希望在每行的每个名称下的第二行显示分类法的内容。假设现在是(3列/行):第1行:名称1 |评级|添加到购物车按钮第2行:名称2 |评级|添加到购物车按钮我希望它是:行1:名称1 |评级|添加到购物车按钮分类国家/地区||第2行:名称2 |评级|添加到购物车