所以我在Tonya Mork@KnowTheCode 和atutorial 关于改编Rachel Carden的快速编辑。
我需要重构代码,仍然有很多重复,但我基本上是一个Javascript/jQuery新手,所以请耐心等待。
无论如何,解决方案如下:
我的自定义管理css,保存在我的子主题中为product-admin-quick-edit.css
:
ul.fashion-category-checklist,
ul.fashion-brand-checklist,
ul.fashion-season-checklist,
ul.gender-checklist,
ul.food-drink-category-checklist,
ul.food-drink-brands-checklist,
ul.country-of-origin-checklist {
display: none;
}
body.wp-admin span.inline-edit-categories-label {
display: none;
}
然后我将jQuery保存在我的子主题/js文件夹中
product-admin-quick-edit-jquery.js
:
(function($) {
// we create a copy of the WP inline edit post function
var $wp_inline_edit = inlineEditPost.edit;
// and then we overwrite the function with our own code
inlineEditPost.edit = function( id ) {
// "call" the original WP edit function
// we don\'t want to leave WordPress hanging
$wp_inline_edit.apply( this, arguments );
// now we take care of our business
// get the post ID
var $post_id = 0;
if ( typeof( id ) == \'object\' )
$post_id = parseInt( this.getId( id ) );
if ( $post_id > 0 ) {
var productCategoryQuickEdit = jQuery(\'ul.product_cat-checklist\');
var fashionQuickEditCheckBox = jQuery(\'ul.product_cat-checklist li label input#in-product_cat-46:checkbox\');
var fashionCategoryQuickEdit = jQuery(\'ul.fashion-category-checklist\');
var fashionBrandQuickEdit = jQuery(\'ul.fashion-brand-checklist\');
var fashionSeasonQuickEdit = jQuery(\'ul.fashion-season-checklist\');
var fashionGenderQuickEdit = jQuery(\'ul.gender-checklist\');
var foodDrinkQuickEditCheckBox = jQuery(\'ul.product_cat-checklist li label input#in-product_cat-45:checkbox\');
var foodDrinkCategoryQuickEdit = jQuery(\'ul.food-drink-category-checklist\');
var foodDrinkBrandsQuickEdit = jQuery(\'ul.food-drink-brands-checklist\');
var foodDrinkCountryQuickEdit = jQuery(\'ul.country-of-origin-checklist\');
productCategoryQuickEdit.prev().prev().css("display", "block");
if (fashionQuickEditCheckBox.is(\':checked\')) {
fashionCategoryQuickEdit.css("display", "block");
fashionBrandQuickEdit.css("display", "block");
fashionSeasonQuickEdit.css("display", "block");
fashionGenderQuickEdit.css("display", "block");
fashionCategoryQuickEdit.prev().prev().css("display", "block");
fashionBrandQuickEdit.prev().prev().css("display", "block");
fashionSeasonQuickEdit.prev().prev().css("display", "block");
fashionGenderQuickEdit.prev().prev().css("display", "block");
}
fashionQuickEditCheckBox.change(function(){
if (this.checked) {
fashionCategoryQuickEdit.css("display", "block");
fashionBrandQuickEdit.css("display", "block");
fashionSeasonQuickEdit.css("display", "block");
fashionGenderQuickEdit.css("display", "block");
fashionCategoryQuickEdit.prev().prev().css("display", "block");
fashionBrandQuickEdit.prev().prev().css("display", "block");
fashionSeasonQuickEdit.prev().prev().css("display", "block");
fashionGenderQuickEdit.prev().prev().css("display", "block");
} else {
fashionCategoryQuickEdit.css("display", "none");
fashionBrandQuickEdit.css("display", "none");
fashionSeasonQuickEdit.css("display", "none");
fashionGenderQuickEdit.css("display", "none");
fashionCategoryQuickEdit.prev().prev().css("display", "none");
fashionBrandQuickEdit.prev().prev().css("display", "none");
fashionSeasonQuickEdit.prev().prev().css("display", "none");
fashionGenderQuickEdit.prev().prev().css("display", "none");
}
});
if (foodDrinkQuickEditCheckBox.is(\':checked\')) {
foodDrinkCategoryQuickEdit.css("display", "block");
foodDrinkBrandsQuickEdit.css("display", "block");
foodDrinkCountryQuickEdit.css("display", "block");
foodDrinkCategoryQuickEdit.prev().prev().css("display", "block");
foodDrinkBrandsQuickEdit.prev().prev().css("display", "block");
foodDrinkCountryQuickEdit.prev().prev().css("display", "block");
}
foodDrinkQuickEditCheckBox.change(function(){
if (this.checked) {
foodDrinkCategoryQuickEdit.css("display", "block");
foodDrinkBrandsQuickEdit.css("display", "block");
foodDrinkCountryQuickEdit.css("display", "block");
foodDrinkCategoryQuickEdit.prev().prev().css("display", "block");
foodDrinkBrandsQuickEdit.prev().prev().css("display", "block");
foodDrinkCountryQuickEdit.prev().prev().css("display", "block");
} else {
foodDrinkCategoryQuickEdit.css("display", "none");
foodDrinkBrandsQuickEdit.css("display", "none");
foodDrinkCountryQuickEdit.css("display", "none");
foodDrinkCategoryQuickEdit.prev().prev().css("display", "none");
foodDrinkBrandsQuickEdit.prev().prev().css("display", "none");
foodDrinkCountryQuickEdit.prev().prev().css("display", "none");
}
});
}
};
})(jQuery);
然后我将这些文件放入
functions.php
:
add_action( \'admin_enqueue_scripts\', \'load_my_custom_wp_admin_style\' );
function load_my_custom_wp_admin_style() {
wp_register_style( \'product_admin_quick_edit_css\', get_stylesheet_directory_uri() . \'/product-admin-quick-edit.css\', false, \'1.0.0\' );
wp_enqueue_style( \'product_admin_quick_edit_css\' );
}
add_action( \'admin_print_scripts-edit.php\', \'load_custom_quick_edit_scripts\', 9999, 2 );
function load_custom_quick_edit_scripts(){
wp_enqueue_script( \'product-admin-quick-edit-jquery\', get_stylesheet_directory_uri() . "/js/product-admin-quick-edit-jquery.js", array( \'jquery\', \'inline-edit-post\' ), CHILD_THEME_VERSION, true );
}
确保在晚些时候将脚本排队
9999
, 让他们依赖
inline-edit-post
来自WP Core的javascript。
我将把jQuery重构成某种循环,以添加和删除css类,这应该会大大减少它。
希望它能帮助其他人,我找不到任何其他关于在依赖于其他分类设置的快速编辑中动态显示分类的材料,所以我猜不太需要它。