是的,完成此操作的方法是在wp.customize.previewedDevice
值更改。
因此,您可以在customize_controls_enqueue_scripts
与的操作customize-controls
作为其脚本依赖项,它执行以下操作:
(function ( api ) {
api.bind( \'ready\', function() {
function sendPreviewedDevice() {
api.previewer.send( \'previewed-device\', api.previewedDevice.get() );
}
// Send the initial previewed device when preview is ready.
api.previewer.bind( \'ready\', sendPreviewedDevice );
// Send the previewed device whenever it changes.
api.previewedDevice.bind( sendPreviewedDevice );
});
}) ( wp.customize );
处理将消息从控件发送到预览的。然后,您需要在预览中使用单独的脚本来侦听来自窗格的消息。将单独的脚本排入
wp_enqueue_scripts
如果
is_customize_preview()
例如:
(function ( api, $ ) {
api.bind( \'preview-ready\', function() {
var previousPreviewedDevice = null;
// Listen for a previewed-device message.
api.preview.bind( \'previewed-device\', function( previewedDevice ) {
var body = $( document.body );
if ( previousPreviewedDevice ) {
body.removeClass( previousPreviewedDevice );
}
body.addClass( previewedDevice );
previousPreviewedDevice = previewedDevice;
});
});
}) ( wp.customize, jQuery );
结果是,正在预览的设备将作为类显示在预览窗口的
body
元素,例如
tablet
.
我在一个要点中把它推到了一个独立的插件中:https://gist.github.com/westonruter/b2fdf9347c8441ba9933569b15e2faa7
有关更多信息,请参阅Customizer Responsive Server-Side Components Device Preview 插件及其对应的writeup:Previewing Themes with Adaptive Designs in the Customizer. 这可能更接近您想要的,因为您似乎没有实现一个纯粹的响应主题。