Hai, saya telah mengunduh plugin "Daftar Staf Sederhana" dan plugin tersebut melakukan apa yang saya perlukan, tetapi saya tidak ingin editor membuat subkategori. Bagaimana cara menghapus/menyembunyikan kotak pilih "Induk" pada formulir?
WordPress: Hapus dropdown Induk dari formulir kategori
Jawaban (6)
Tambahkan kode di bawah ini di file function.php
tema Anda saat ini.
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
}
Anda dapat menggunakan pengaturan opsi ini di register_taxonomy()
fungsi
'hierarchical' => false,
'parent_item' => null,
'parent_item_colon' => null,
Ini akan menghapus bidang induk.
'hierarchical' => false
mengubah kotak kategori menjadi kotak tag (dan tag tentu saja tidak boleh memiliki tag induk), tetapi 'parent_item' => null, 'parent_item_colon' => null,
termasuk dalam larik labels
dan tidak boleh berada dalam larik yang sama dengan parameter hierarchical
.
- person certainlyakey; 21.01.2018
Ini akan menghapus dropdown induk dari taksonomi dan layar posting baru/edit.
<?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' );
Jika Anda ingin menonaktifkan "hierarki" itu sendiri dari taksonomi kategori, tambahkan kode ini di function.php
.
add_action('init', function(){
global $wp_taxonomies;
$wp_taxonomies['category']->hierarchical = false;
});
Tambahkan kode di bawah ini di file function.php tema Anda saat ini.
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 );
Referensi: https://codex.wordpress.org/Function_Reference/register_taxonomy
Ini akan bekerja dengan WordPress 5.4.2. Bagi saya, semua solusi lain menampilkan bidang tersebut selama jQuery menghapusnya. Solusi cepat dan kotor saya bersembunyi melalui CSS dan menghapusnya dengan jQuery. Sayangnya hanya menyembunyikan (tidak menghapus) yang tampaknya berfungsi dengan Editor Gutenberg. Mungkin orang lain punya solusi lain.
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' );
Ah ngomong-ngomong - JANGAN gunakan kode berikut karena Anda akan mendapat masalah buruk dengannya. Saat Anda menyimpan postingan tanpa mengubah kategori, semua kategori postingan ini akan dihapus dan ID kategori akan dibuat sebagai kategori baru dan ditambahkan ke postingan Anda.
global $wp_taxonomies;
$wp_taxonomies['category']->hierarchical = false;