【发布时间】:2014-07-12 07:57:53
【问题描述】:
您好,我已经下载了一个插件“Simple Staff List”,它可以满足我的需要,但我不希望编辑创建子类别。如何删除/隐藏表单上的“父”选择框?
【问题讨论】:
-
哎呀!一定是个男人的样子……刚刚发现这个:wordpress.stackexchange.com/questions/58799/…
标签: wordpress categories
您好,我已经下载了一个插件“Simple Staff List”,它可以满足我的需要,但我不希望编辑创建子类别。如何删除/隐藏表单上的“父”选择框?
【问题讨论】:
标签: wordpress categories
在您当前的主题 function.php 文件中添加以下代码。
add_action( 'admin_head-edit-tags.php', 'wpse_58799_remove_parent_category' );
function wpse_58799_remove_parent_category()
{
if ( 'category' != $_GET['taxonomy'] )
return;
$parent = 'parent()';
if ( isset( $_GET['action'] ) )
$parent = 'parent().parent()';
?>
<script type="text/javascript">
jQuery(document).ready(function($)
{
$('label[for=parent]').<?php echo $parent; ?>.remove();
});
</script>
<?php
}
【讨论】:
您可以使用设置这些选项
register_taxonomy()函数
'hierarchical' => false,
'parent_item' => null,
'parent_item_colon' => null,
这将删除父字段。
【讨论】:
'hierarchical' => false 确实将分类框转换为标签框(标签自然不能有父标签),但'parent_item' => null, 'parent_item_colon' => null, 属于labels 数组,不能与hierarchical 参数在同一个数组中。跨度>
这将从分类和发布新/编辑屏幕中删除父下拉列表。
<?php
function remove_tax_parent_dropdown() {
$screen = get_current_screen();
if ( 'category' == $screen->taxonomy ) {
if ( 'edit-tags' == $screen->base ) {
$parent = "$('label[for=parent]').parent()";
} elseif ( 'term' == $screen->base ) {
$parent = "$('label[for=parent]').parent().parent()";
}
} elseif ( 'post' == $screen->post_type ) {
$parent = "$('#newcategory_parent')";
} else {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
<?php echo $parent; ?>.remove();
});
</script>
<?php
}
add_action( 'admin_head-edit-tags.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-term.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-post.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-post-new.php', 'remove_tax_parent_dropdown' );
【讨论】:
如果您想从类别分类中禁用“分层”本身,请将此代码添加到您的 function.php。
add_action('init', function(){
global $wp_taxonomies;
$wp_taxonomies['category']->hierarchical = false;
});
【讨论】:
在您当前的主题 function.php 文件中添加以下代码。
function custom_taxonomy() {
$labels = array(
'name' => _x( 'Brands', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Brand', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Taxonomy', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'New Item Name', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'parent_item' => null,
'parent_item_colon' => null,
);
register_taxonomy( 'brands', array( 'product' ), $args );
}
add_action( 'init', 'custom_taxonomy', 0 );
参考:https://codex.wordpress.org/Function_Reference/register_taxonomy
【讨论】:
这适用于 WordPress 5.4.2。对我来说,只要 jQuery 删除它们,所有其他解决方案都会显示这些字段。我快速而肮脏的解决方案通过 CSS 隐藏并使用 jQuery 删除它们。不幸的是,只有隐藏(而不是删除)似乎适用于古腾堡编辑器。也许其他人有其他解决方案。
function remove_tax_parent_dropdown() {
$screen = get_current_screen();
if ( 'category' == $screen->taxonomy ) {
if ( 'edit-tags' == $screen->base ) {
$parent = "$('label[for=parent]').parent().remove(); ";
$css = ".term-parent-wrap{display:none;}";
} elseif ( 'term' == $screen->base ) {
$parent = "$('label[for=parent]').parent().parent().remove(); ";
$css = ".term-parent-wrap{display:none;}";
}
} elseif ( 'post' == $screen->post_type ) {
$parent = "$('#newcategory_parent').remove();";
$css = "div.components-base-control:nth-child(3){display:none;}";
} else {
return;
}
if(!empty($css)) {
echo '<style type="text/css">';
echo $css;
echo '</style>';
}
if(!empty($parent)) {
echo '<script type="text/javascript">';
echo 'jQuery(document).ready(function($) {';
echo $parent;
echo '});';
echo '</script>';
}
}
add_action( 'admin_head-edit-tags.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-term.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-post.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-post-new.php', 'remove_tax_parent_dropdown' );
顺便说一句 - 不要使用下面的代码,因为你会遇到一些不好的问题。当您保存帖子而不更改类别时,该帖子的所有类别都将被删除,类别 ID 将作为新类别创建并添加到您的帖子中。
global $wp_taxonomies;
$wp_taxonomies['category']->hierarchical = false;
【讨论】: