好的,您可以使用一些自定义控件查看我的回购协议
https://github.com/dingo-d/wordpress-theme-customizer-extra-custom-controls
我在这里改编了《威斯顿-罗特斯指南》中的上下文控制:
https://make.xwp.co/2016/07/24/dependently-contextual-customizer-controls/
在我的回购示例中,我有一个boxed body复选框控件,用于切换boxed body。
您需要将一个脚本排队以将js代码放入
add_action( \'customize_controls_enqueue_scripts\', \'mytheme_customizer_control_toggle\' );
/**
* Custom contextual controls
*
* @since 1.0.0
*/
function mytheme_customizer_control_toggle() {
wp_enqueue_script( \'mytheme-contextualcontrols\', get_template_directory_uri() . \'/inc/customizer/js/customizer-contextual.js?v=\' . rand(), array( \'customize-controls\' ), false );
}
在你的
customizer-contextual.js
您将获得类似以下内容:
( function( api ) {
\'use strict\';
api.bind( \'ready\', function() {
api( \'boxed_body\', function(setting) {
var linkSettingValueToControlActiveState;
/**
* Update a control\'s active state according to the boxed_body setting\'s value.
*
* @param {api.Control} control Boxed body control.
*/
linkSettingValueToControlActiveState = function( control ) {
var visibility = function() {
if ( true === setting.get() || 1 === setting.get() ) {
control.container.slideDown( 180 );
} else {
control.container.slideUp( 180 );
}
};
// Set initial active state.
visibility();
// Update activate state whenever the setting is changed.
setting.bind( visibility );
};
// Call linkSettingValueToControlActiveState on the border controls when they exist.
api.control( \'boxed_body_border_width\', linkSettingValueToControlActiveState );
api.control( \'boxed_body_border_color\', linkSettingValueToControlActiveState );
api.control( \'boxed_body_border_style\', linkSettingValueToControlActiveState );
});
});
}( wp.customize ) );
您的目标是控件名称(在本例中为
\'boxed body\'
), 在其中,您可以检查该控件是否处于活动状态,并相应地设置其他链接控件的“可见性”。
您还可以通过挂接到在预览屏幕中切换此更改customize_preview_init
钩
add_action( \'customize_preview_init\', \'mytheme_customizer_live_preview\' );
/**
* Live preview script enqueue
*
* @since 1.0.0
*/
function mytheme_customizer_live_preview() {
wp_enqueue_script( \'mytheme-themecustomizer\', get_template_directory_uri() . \'/inc/customizer/js/customizer.js?v=\' . rand(), array( \'jquery\', \'customize-preview\' ), false );
}
内部
customizer.js
您将获得类似以下内容:
(function($, api) {
\'use strict\';
// Boxed Body Toggle.
api( \'boxed_body\', function(value) {
value.bind(function(newval) {
if (newval) {
$( \'body\' ).wrapInner( \'<div class="boxed_body_wrapper" />\' );
} else {
$( \'.boxed_body_wrapper\' ).contents().unwrap();
}
});
});
})(jQuery, wp.customize);
希望这有帮助:)